Meteor.users.update 向集合中的当前文档添加新字段时访问被拒绝

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

我正在寻找向当前文档添加一个新字段(将来会用 $push 填充的数组)但是我收到错误 update failed: MongoError: Cannot apply $push/$pushAll modifier to non-array

我正在制作 Meteor.users 系列。

代码 运行 是:

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

发生这种情况是因为您不应该更改用户对象的根字段。来自 the docs:

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

所以你可以

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

请注意,您应该 limit what you store in profile

您需要将"newfield"定义为数组,否则操作将失败。看这里:http://docs.mongodb.org/manual/reference/operator/update/push/#up._S_push