我收到此错误,我不知道如何解决它。 在节点js服务器中,我在promise上使用了一些.then()函数,最后我放置了一个由于某种原因无法识别的.catch()函数。 我在教程的许多地方都看到过这是错误的处理方式。 我没有使用任何外部库。
错误:
TypeError:promise.then(...)。then(...)。then(...)。then(...)。catch不是函数这是我的代码:
exports.joinAlbum = function(req,res){ var promise = Album.findOne()。where({'shortId':req.body.shortId})。exec() ; //返回一个promise promise.then(function(albumDoc){ console.log('Then#1'); 。 .... } 返回albumDoc.save(); //返回一个承诺}) .then(function(albumDoc){ console.log('Then#2'); ..... 返回User.findById( req.body.userId).exec(); //返回一个承诺}) .then(function(userDoc){ console.log( '然后#3'); ........ 返回userDoc.save(); //返回一个承诺}) //返回响应 .then(function(savedUserDoc){ console.log('Then#4'); 返回res.status(200) .json({success:true}); }) //错误处理程序 .catch(function(err){ console.log('Catch #1'); console.log(错误); res.status(200).json({success:false}); }); }如果.catch()不是处理承诺错误的正确方法,那么你有没有建议?我试图避免使用外部库并且更喜欢使用原生javascript
编辑:解决方案
<我添加了一个名为blue-bird的npm模块帮我解决了这个问题。解决方案看起来你正在使用Mongoose ,返回自己的Promise,而不是包含catch函数的ES6 promise。猫鼬Promise没有捕捉功能。你可以覆盖Mongoose使用的默认Promise,幸运的是:
eddywashere/blog/switching-out-callbacks-with-promises-in-mongoose/
Im getting this error and i dont know how to solve it. In a node js server, i'm using some .then() function on a promise and at the end i placed a .catch() function that for some reason is not recognized. I have seen in many places in tutorials that this is how an error is handled. I'm not using any external libraries.
The error :
TypeError: promise.then(...).then(...).then(...).then(...).catch is not a functionThis is my code:
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}); }); }If .catch() is not the correct way to handle promise error, what do you suggest? Im trying to avoid using external libraries and prefer using native javascript
EDIT: solution
I added a npm module called blue-bird that helped me solve this.
解决方案It looks like you're using Mongoose, which returns its own Promise, not the ES6 promise which includes a catch function. A mongoose Promise has no catch function. You can overwrite the default Promise that Mongoose uses, fortunately:
eddywashere/blog/switching-out-callbacks-with-promises-in-mongoose/
更多推荐
TypeError:promise.then(...)。then(...)。then(...)。then(...)。catch不是Node Js中的函数
发布评论