Ben Bals
Ben Bals

Reputation: 25

Meteor Collection empty on server

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 //appears to be empty
    console.log Meteor.userId()

    Meteor.call 'hasBasicPermissions', (err,res) ->
      console.log res // false
      if res
        Articles.update(id, obj)
      else
        Meteor.call('notAuthorisedError') // throws the error
isEditor: ->
  !!Editors.findOne {userId: Meteor.userId()} // return true when called in Meteor shell, but is false when used in the method, but only on the server side
isAdmin: ->
  !!Admins.findOne {userId: Meteor.userId()}
hasBasicPermissions: ->
  console.log(Meteor.userId())
  console.log(Admins)
  res = !!Admins.findOne({userId: "9fZmAnoJubaGDZH64"}) or !!Admins.findOne({userId: "9fZmAnoJubaGDZH64"})
  console.log res
  return res
notAuthorisedError : ->
  throw new Meteor.Error("not-authorized");

Upvotes: 1

Views: 331

Answers (1)

Geoffrey Booth
Geoffrey Booth

Reputation: 7366

I would guess that you’re defining your collection (the @Admins = new Meteor.Collection('admins') or similar line) in a place where the code is only executed on the client: within a file in the client folder, or inside a Meteor.isClient block. Try putting it in shared code, for example in a lib or collections folder.

Upvotes: 1

Related Questions