gpt4 book ai didi

javascript - Meteor:Meteor 方法中的 this.userId 和 Meteor.userId() 均为 null

转载 作者:行者123 更新时间:2023-11-28 10:48:06 26 4
gpt4 key购买 nike

为了将此问题归结为基本问题和最少的代码,我创建了一个简单的新 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.userIdMeteor.userId()正在返回null .

这是 meteor 错误还是这种方法错误?

最佳答案

这是预期的行为。这是因为您正在从服务器调用该方法。服务器上没有用户,因此方法调用对象 (this) 上的 userId 为 null。登录后尝试从客户端调用它。

关于javascript - Meteor:Meteor 方法中的 this.userId 和 Meteor.userId() 均为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39200445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com