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

标签 javascript meteor

为了将此问题归结为基本问题和最少的代码,我创建了一个简单的新 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/

相关文章:

javascript - Angular JS : input[radio] doesn't work

javascript - 将模板实例数据作为关键字参数传递给嵌套模板

mongodb - 将客户端日期作为 UTC 日期对象保存到 Mongo 中

authentication - 在 Meteor 中创建没有密码的用户

node.js - 让 Blade 在 Windows 8 上与 Meteor 配合使用

Javascript POST 不适用于 .htaccess

javascript - Rails 5 - JS 无法在生产环境中工作(仅限)

javascript - 如何获取多个复选框值并分配给隐藏字段

Eclipse 中的 Javascript 功能

meteor - 错误 : No uiManager configured on Router