【问题标题】:Node Promise - TypeError cannot read property .then of undefined节点承诺 - TypeError 无法读取未定义的属性 .then
【发布时间】:2017-07-19 05:14:40
【问题描述】:

下面两段代码抛出类型错误:

TypeError: 无法读取未定义的属性“then”。

我觉得我缺少一些基本的东西。值得注意的是,第二段代码中的“结果”日志是在抛出错误后完成的。这让我相信我可能做错了涉及异步的事情。但是,即使在阅读了建议的问题之后,我也无法理解它。

任何帮助将不胜感激!

router.route('/user/:id')

    .put(auth.authRest, function(req, res) {
      userManager.updateUser(req.params.id, req.body)
        .then(function(response) { // line where error is thrown
          res.send({data:response});
        });
    });

来自用户管理器:

this.updateUser = function(id, data) {
    User.findOne({ _id: id }).exec(function(err, user){
      if(err) {
        console.log(err);
      } else {
        for(let prop in data) {
          user[prop] = data[prop];
        }

        var result = user.save().catch(function(err){
          console.log(err);
        });

        console.log(result); // this log is done below the error, it does contain a promise
        return result;
      } 
    }).catch(function(err){
      console.log(err);
    });

  };

【问题讨论】:

  • 您不会从this.updateUser 返回Promise。 exec 是否返回 Promise?
  • 嗯,当记录结果时它告诉我它包含一个承诺,怎么会?
  • return result; 属于你传递给exec 的回调,而不是你分配给this.updateUser 的函数。

标签: javascript node.js mongoose promise


【解决方案1】:

如果你想使用 Promise,你需要从 this.updateUser 返回一个 Promise,return result 属于你传递给 exec 的回调,而不是你分配给 this.updateUser 的函数。

this.updateUser = function(id, data) {
  return User.findOne({
    _id: id
  }).exec().then(function(user) {
    for (let prop in data) {
      user[prop] = data[prop];
    }

    var result = user.save().catch(function(err) {
      console.log(err);
    });

    console.log(result); // this log is done below the error, it does contain a promise
    return result;
  }).catch(function(err) {
    console.log(err);
  });

};

根据您希望如何进行错误处理,您可以将其缩小到:

this.updateUser = function(id, data) {
  return User.findOne({
    _id: id
  }).exec().then(function(user) {
    for (let prop in data) {
      user[prop] = data[prop];
    }

    return user.save();
  }).catch(function(err) {
    console.log(err);
  });
};

【讨论】:

  • @BenFortune 是的,绝对的,谢谢你的提示,我不小心把它删了。
【解决方案2】:

'updateUser' 方法应该返回一个 Promise,这样第一个方法中的 .then 调用才会起作用。

试试下面的方法(使用节点包'q')

this.updateUser = function(id, data) {
    var deferred = Q.defer()
    User.findOne({ _id: id }).exec(function(err, user){
      if(err) {
        console.log(err);
        deferred.reject(err)
      } else {
        for(let prop in data) {
          user[prop] = data[prop];
        }

        var result = user.save().catch(function(err){
          console.log(err);
        });

        console.log(result); // this log is done below the error, it does contain a promise
        deferred.resolve(resolve)
        //return result;
      } 
    }).catch(function(err){
        deferred.reject(err)
      console.log(err);
    });

    return deferred.promise

  };

【讨论】:

  • @BenFortune:问题不在于使用哪种承诺模式。它是导致问题的原因,在这种情况下,理想情况下是缺少返回承诺 - “TypeError:无法读取属性”然后'未定义的"。
  • 您的回答确实解决了问题。但是使用每个 Promise 库供应商都建议/坚持不要再使用的模式是你应该避免的。
猜你喜欢
  • 2018-11-30
  • 2020-03-12
  • 1970-01-01
  • 2019-03-25
  • 2019-04-23
  • 2017-07-12
  • 2017-01-23
  • 2019-08-19
  • 2014-09-07
相关资源
最近更新 更多