Meteor 如何检查流星';s的用户帐户为空?
我试图在Meteor应用程序启动时创建一个默认用户,因此我检查users集合中是否有任何用户,但它不起作用 我试试这个:Meteor 如何检查流星';s的用户帐户为空?,meteor,Meteor,我试图在Meteor应用程序启动时创建一个默认用户,因此我检查users集合中是否有任何用户,但它不起作用 我试试这个: if (Meteor.users.find().count() === 0) { seedUserId = Accounts.createUser({ email: 'f@oo.com', password: '123456' }); } 此count()返回0,但在Mongo中我有用户: meteor:PRIMARY>db.users.find()
if (Meteor.users.find().count() === 0) {
seedUserId = Accounts.createUser({
email: 'f@oo.com',
password: '123456'
});
}
此count()返回0,但在Mongo中我有用户:meteor:PRIMARY>db.users.find().count()
>2
我也尝试过:Meteor.users.findOne()
但是返回未定义的
我做错了什么???您可能会看到一种竞争条件,即当您的代码执行时,服务器没有完全加载——尝试将代码包装在Meteor.startup中
if (Meteor.isServer) {
Meteor.startup(function () {
if (Meteor.users.find().count() === 0) {
seedUserId = Accounts.createUser({
email: 'f@oo.com',
password: '123456'
});
}
});
}
您是在服务器端还是客户端执行此操作?服务器端@Soren