Javascript 在集合中按ID查找用户。允许在Meteor中行为异常
我只想允许用户在验证其电子邮件时插入文档,我编写了以下代码Javascript 在集合中按ID查找用户。允许在Meteor中行为异常,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,我只想允许用户在验证其电子邮件时插入文档,我编写了以下代码 Events.allow({ insert: function (userId, doc) { var user = Meteor.users.find({_id: userId}); console.log(user); // logs a large object // if the user's email is verified if (user.email
Events.allow({
insert: function (userId, doc) {
var user = Meteor.users.find({_id: userId});
console.log(user); // logs a large object
// if the user's email is verified
if (user.emails[0].verified) {
return true;
}
return false;
}
});
运行此操作时,我得到一个错误,“internal server error”
,查看服务器时,我得到TypeError:无法读取未定义的
的属性“0”,这意味着我的用户对象有问题。所以我记录了这个对象,没有看到用户,而是得到了一个巨大的对象,缩写如下:
{ _mongo:
// lots of stuff
}
我认为这是Meteor使用的顶级mongoDB对象
Meteor.users.find({u id:userId})
返回MongoDB对象而不是我要找的用户,这到底是怎么回事?您可能在找findOne
,而不是find
find
返回指向结果的光标,findOne
返回第一个匹配的文档