TypeError:promise.then(...)。then(...)。then(...)。then(...)。catch不是Node Js中的函数 [英] TypeError: promise.then(...).then(...).then(...).then(...).catch is not a function in Node Js

查看:393
本文介绍了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,幸运的是:



http://eddywashere.com/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:

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

这篇关于TypeError:promise.then(...)。then(...)。then(...)。then(...)。catch不是Node Js中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆