简体   繁体   English

节点Promise-TypeError无法读取未定义的属性.then

[英]Node Promise - TypeError cannot read property .then of undefined

The two pieces of code below throw a type error: 下面的两段代码引发类型错误:

TypeError: Cannot read property 'then' of undefined. TypeError:无法读取未定义的属性“ then”。

I feel like I'm missing something fundamental. 我觉得我缺少基本的东西。 Remarkable is that the log of 'result' in the second piece of code is done after the error is thrown. 值得注意的是,第二条代码中的“结果”的日志是在引发错误之后完成的。 Which leads me to believe I might be doing something wrong involving asynch. 这使我相信,我可能在做涉及庇护的事情时出错了。 However I cannot get my head around it, even after reading the suggested questions. 但是,即使阅读了建议的问题,我也仍然无法解决。

Any help would be greatly appreciated! 任何帮助将不胜感激!

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});
        });
    });

and from userManager: 并从userManager中:

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);
    });

  };

If you want to use Promises you need to return a Promise from this.updateUser , the return result belongs to the callback you pass to exec and not to the function you assigned to this.updateUser . 如果要使用Promises,则需要从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);
  });

};

Depending on how you want to do the error handling you could shrink it down to: 根据您要如何执行错误处理,可以将其缩减为:

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);
  });
};

'updateUser' method should return a promise, so that .then call in the first method would work. 'updateUser'方法应返回一个Promise,以便第一个方法中的.then调用起作用。

Try something like below ( used node package 'q') 尝试以下类似操作(使用的节点包“ 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

  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法读取未定义(Promise)(node.js)的属性“ then” - TypeError: Cannot read property 'then' of undefined (Promise) (node.js) React Promise-TypeError:无法读取未定义的属性“ then” - React Promise - TypeError: Cannot read property 'then' of undefined TypeError:无法读取未定义的Dialogflow Promise的属性'then' - TypeError: Cannot read property 'then' of undefined Dialogflow Promise Promise - TypeError:无法读取未定义的属性'then' - Promise - TypeError: Cannot read property 'then' of undefined TypeError:无法读取未定义的属性“ then”(返回承诺?) - TypeError: Cannot read property 'then' of undefined (return promise?) TypeError:无法读取 promise 中未定义的属性“userId”? - TypeError: Cannot read property 'userId' of undefined in promise? 类型错误:无法读取未定义的属性“节点” - TypeError: Cannot read property 'node' of undefined 节点 - 类型错误:无法读取未定义的属性“问题” - node - TypeError: Cannot read property 'question' of undefined 节点:TypeError:无法读取未定义的属性“ on” - Node: TypeError: Cannot read property 'on' of undefined 节点:TypeError:无法读取未定义的属性“替换” - Node: TypeError: Cannot read property 'replace' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM