[SOLVED] Meteor Collection empty

I got a collection named Admins and I use Admins.findOne({userId: Meteor.userID}) to check if a user is an admin and it works fine if I do it when I use it in the meteor shell and when I call the method I have defined for it from the browser console, but when I do server-side permission checks with it the Admins collection appears to be empty (Admins._collection._docs._map is empty. I have checked.). I don’t know why my method can’t access it if I perform it on the server side, but works fine when I call it form the chrome dev tools. My methods are:

updateArticle: (id, obj) ->

    console.log Admins
    console.log Meteor.userId()

    Meteor.call 'hasBasicPermissions', (err,res) ->
      console.log res
      if res
        Articles.update(id, obj)
isEditor: ->
  !!Editors.findOne {userId: Meteor.userId()}
isAdmin: ->
  !!Admins.findOne {userId: Meteor.userId()}
hasBasicPermissions: ->
  res = !!Admins.findOne({userId: "9fZmAnoJubaGDZH64"}) or !!Admins.findOne({userId: "9fZmAnoJubaGDZH64"})
  console.log res
  return res
notAuthorisedError : ->
  throw new Meteor.Error("not-authorized");

Does your code mean Meteor.methods({}) definition? If so I gues the next:

  1. You shouldn call method inside method
  2. Note that Meteor.userId() works on a client only. On the server you can access for userId only inside methods and publications, and you should call it as this.userId - means user id for that call. See the docs
  3. It is not good idea to use findOne on the server side, try Admins.find(userId: @userId).count() isnt 0 instead
1 Like

Thanks for the good and quick answer. Jup this is my meteor.methods definition.

Not at all))) Please mark topic with [SOLVED] if your issue is done