TypeError:promise.then(…).then(…).then(…).then(…).catch不是Node Js中的函数

我得到这个错误,我不知道如何解决它。 在一个节点的js服务器,我使用一些承诺的.then()函数,并在最后我放置了一个.catch()函数,由于某种原因无法识别。 我在很多地方看过教程,这是如何处理错误的。 我没有使用任何外部库。

错误 :

TypeError: promise.then(...).then(...).then(...).then(...).catch is not a function 

这是我的代码:

 exports.joinAlbum = function(req, res){ var promise = Album.findOne().where({'shortId': req.body.shortId}).exec(); // Returns a promise promise.then(function(albumDoc){ console.log('Then #1'); ..... } return albumDoc.save(); // Returns a promise }) .then(function(albumDoc){ console.log('Then #2'); ..... return User.findById(req.body.userId).exec(); // Returns a promise }) .then(function(userDoc){ console.log('Then #3'); ........ return userDoc.save(); // Returns a promise }) // Return a response .then(function(savedUserDoc){ console.log('Then #4'); return res.status(200).json({success: true}); }) //Error handler .catch(function(err){ console.log('Catch #1'); console.log(err); res.status(200).json({success: false}); }); } 

如果.catch()不是处理承诺错误的正确方法,那么您有什么build议? 我试图避免使用外部库,更喜欢使用原生的JavaScript

编辑:解决scheme

我添加了一个名为蓝鸟的npm模块,帮助我解决了这个问题。

看起来你正在使用Mongoose,它会返回自己的Promise,而不是包含catch函数的ES6承诺。 mongoose诺言没有捕捉function。 幸运的是,您可以覆盖Mongoose使用的默认Promise:

http://eddywashere.com/blog/switching-out-callbacks-with-promises-in-mongoose/