0

I'm trying to make my meteor-app for REST services available. For this I use the package "Restivus" which also works just fine. But once I'd like to run a meteor method this.userId is undefined.

Api.addRoute('addArticle', {authRequired: true}, {
        post: function () {
            console.log(this.userId); //<-- hwuqtFXf8aKperJ5p
            try {
                Meteor.call("addArticle",this.bodyParams);
            } catch (e) {
                return {code:500,type:e.error,reason:e.reason};
            }
        }
    });

the method:

new ValidatedMethod({
    name: 'addArticle',
....
if (!this.userId) {
        throw new Meteor.Error(...); //is thrown
    }

What am I doing wrong?

laren0815
  • 265
  • 1
  • 5
  • 22

1 Answers1

0

In Meteor methods you get the current userId by doing

    Meteor.userId() 

and not

    this.userId

So you would need to update your code to

    if(!Meteor.userId()){ 
        throw new Meteor.Error(403, '403:Forbidden', 'You shall not pass!')
    }
Robert Fines
  • 700
  • 4
  • 13
  • Perhaps this used to work, but it also doesn't work for me, I can see the user and userId in the callback for the route, but not in the meteor method (using `Meteor.userId()`) – Trey Aug 28 '18 at 18:37