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

编程入门 行业动态 更新时间:2024-10-10 22:20:06
本文介绍了TypeError:promise.then(...)。then(...)。then(...)。then(...)。catch不是Node Js中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我收到此错误,我不知道如何解决它。 在节点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 function

This 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中的函数

本文发布于:2023-11-28 04:03:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640879.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   promise   TypeError   catch   Js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!