Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This this.userId() often null when calling from inside a meteor method

Tags:

meteor

I recently started trying out the auth branch of meteor and depending where I call my meteor method that calls this.userId(), it will return null or the user id that I need.

To be a little more specific, it works when my meteor method is called from a coffeescript class initialized inside of Meteor.startup, but not when that same method is called from inside Meteor.publish.

The meteor method is simple and probably not relevant, but just in case:

Meteor.methods(
  get_user_id: ->
    return @userId()
)

EDIT: It seems people were not able to reproduce my problem, here is a patch on the todo auth example that should demonstrate it.

    diff --git a/examples/todos/server/methods.js b/examples/todos/server/methods.js
    index e69de29..d4182a6 100644
    --- a/examples/todos/server/methods.js
    +++ b/examples/todos/server/methods.js
    @@ -0,0 +1,6 @@
    +Meteor.methods({
    +  get_user_id: function() {
    +    console.log(this.userId());
    +    return this.userId();
    +  }
    +});
    diff --git a/examples/todos/server/publish.js b/examples/todos/server/publish.js
    index 1552c5e..87cb29f 100644
    --- a/examples/todos/server/publish.js
    +++ b/examples/todos/server/publish.js
    @@ -16,6 +16,8 @@ Todos = new Meteor.Collection("todos");

     // Publish visible items for requested list_id.
     Meteor.publish('todos', function (list_id) {
    +  Meteor.call('get_user_id');
    +  //console.log(this.userId())
       return Todos.find({
         list_id: list_id,
         privateTo: {

thanks Eitan for the patch!

like image 917
brysgo Avatar asked Mar 12 '26 13:03

brysgo


1 Answers

You should use Meteor.userId() to retrieve the current user's ID within Meteor.methods. :)

like image 102
Patrick Coffey Avatar answered Mar 14 '26 19:03

Patrick Coffey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!