首页 文章

Meteor userId始终为null

提问于
浏览
0

此Meteor应用程序删除了不安全和自动发布,并添加了帐户密码 .
meteor:PRIMARY> db.users.find({}); 还显示我使用的唯一用户凭据的存在 .
调用 Meteor.call('addTasks1',params); 抛出错误,进一步检查显示 Meteor.userId() 正在 null 为什么这样以及如何修复它?谢谢

update 根据Stephen Woods的建议修正案;
当我将服务器上的方法addTasks1从 Meteor.userId() 更改为 this.userId 时,它仍会抛出错误 .

///////////////////////////
//     both/both.js      //
///////////////////////////
Tasks1 = new Mongo.Collection('tasks1');


///////////////////////////
//   client/client.js    //
///////////////////////////
Template.login.events({
  'click #logMe': function() {
     var credentials = [$('#id').val(), $('#pin').val()];
     Meteor.call('logMeIn', credentials);
   }
 });

Template.footer.events({
  'click button': function () {
    if ( this.text === "SUBMIT" ) {
      var inputs = document.getElementsByTagName('input');
      for (var i = 0; i < inputs.length; i++) {
        var params = {};
        params[inputs[i].name] = inputs[i].value;
        Meteor.call('addTasks1', params);
      }
    }
  }
});


///////////////////////////
//    server/server.js   //
///////////////////////////
Meteor.methods({
  logMeIn: function (credentials) {
     Accounts.createUser({username: credentials[0], password: credentials[1]});
  }
});

Meteor.publish('tasks1', function(){
  return Tasks1.find({userId: this.userId});
});

Meteor.methods({
  addTasks1: function (doc) {
    if (Meteor.userId()) {
      Tasks1.insert(doc);
    } else {
      throw new Meteor.Error("Not Authorized");
    }
  }
});

1 回答

  • 0

    您需要将用户登录,在客户端使用 Meteor.loginWithPassword(<USERNAME>, <PASSWORD>) 来记录创建的用户 .

    此外,您的Meteor方法 addTasks1 正在服务器上执行 . 您希望在服务器上使用this.userId而不是Meteor.userId() .

相关问题