【发布时间】:2016-08-31 17:23:52
【问题描述】:
为了将这个问题归结为基本问题和最少的代码,我创建了一个简单的新流星应用程序,添加了 accounts-password 包并添加了以下代码 sn-p。我的数据库中有一个测试用户。
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 中包含正确的 userId。但是在 Meteor Method testMethod 中,this.userId 和 Meteor.userId() 都返回 null。
这是流星虫还是这种方法错误?
【问题讨论】:
-
看起来您的方法在您提供的 sn-p 的全局范围内。 this.userId 只在服务器上工作(我相信你知道),这也会导致嵌套的 Meteor.userId() 失败。
-
发布是在服务器上执行的,根据目前的答案,这实际上是这里的问题。
-
那是正确的发布 => 服务器,订阅 => 客户端。方法 => 服务器,调用 => 客户端 ||服务器
标签: javascript meteor