Search code examples
mongodbmeteormeteor-accountsmongodb-update

Meteor.users.update access denied when adding a new field to current document in collection


I'm looking to add a new field (array that will be populated with $push in the future) to the current document but am getting the error update failed: MongoError: Cannot apply $push/$pushAll modifier to non-array

I'm working on the Meteor.users collection.

The code ran was:

var user = Meteor.userId();
Meteor.users.update({_id:user}, {$set: {"newfield": ["some data"]}});

Solution

  • This happens because you're not supposed to change the root fields of the user object. From the docs:

    By default, the current user's username, emails and profile are published to the client. You can publish additional fields with [...]

    So you can

    Meteor.users.update(user, {$set: {"profile.newfield": ["some data"]}});
    

    Note though that you should limit what you store in profile.