What am I missing? I have this publisher in server
folder:
Meteor.publish("myBooks", () => {
console.log(this.userId);
return Books.find({
owner: this.userId
});
});
this.userId
is always undefined, whether I'm logged in or not. I use Meteor.loginWithFacebook
call (in the client
folder) to log in with my profile.
You are using the fat arrow syntax for defining the function which ties this
to the lexical scope, i.e. the gloabal scope, or the scope of the nearest parent function, in case of NodeJS. Since there probably isn't a userId
defined in the parent scope, you are seeing this.userId
to be undefined
.
Use the function
form to fix the context (i.e. this
) in your code:
Meteor.publish("myBooks", function () {
console.log(this.userId);
return Books.find({
owner: this.userId
});
});