为了将此问题归结为基本问题和最少的代码,我创建了一个简单的新 meteor 应用程序,添加了 accounts-password
打包并添加以下代码片段。我的数据库中有一个测试用户。
Meteor.methods({
'testMethod': function() {
console.log('test2:', this.userId); // => null
console.log('test3:', Meteor.userId()); // => null
}
});
if (Meteor.isServer) {
Meteor.publish('testPublication', function() {
console.log('test1:', this.userId); // => 1c8tHy3zb8vP9E5yb
Meteor.call('testMethod');
});
}
if (Meteor.isClient) {
Meteor.loginWithPassword('test', 'test', function() {
Meteor.subscribe('testPublication');
});
}
如您所见,在出版物 this.userId
中包含正确的用户 ID。但在 meteor 法内testMethod
两者this.userId
和Meteor.userId()
正在返回null
.
这是 meteor 错误还是这种方法错误?
最佳答案
这是预期的行为。这是因为您正在从服务器调用该方法。服务器上没有用户,因此方法调用对象 (this
) 上的 userId 为 null
。登录后尝试从客户端调用它。
关于javascript - Meteor:Meteor 方法中的 this.userId 和 Meteor.userId() 均为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39200445/