[英]I'm getting an Unhandled Promise Rejection error but can't figure out why
const express = require('express');
const cors = require('cors');
const massive = require('massive');
const bodyParser = require('body-parser');
const config = require('../config');
const app = express();
app.use(bodyParser.json());
//massive connection string to database
massive(config.dblink).then(db => {
app.set('db', db)
app.get('db').seed_file().then(res => {
console.log(res)
})
}).catch(err => {
console.log(err)
});
const port = 3001;
app.listen(port, () => {console.log(`the server is listening on ${port}`)})
I am getting the following error: 我收到以下错误:
(node:173676) UnhandledPromiseRejectionWarning: Unhandled promise rejection
(rejection id: 2): error: syntax error at or near "{"
(node:173676) [DEP0018] DeprecationWarning: Unhandled promise rejections are
deprecated. In the future, promise rejections that are not handled will
terminate the Node.js process with a non-zero exit code.
I haven't been able to figure out what is wrong. 我一直无法弄清楚出什么问题了。 I have looked at multiple different examples, but cannot see the problem.
我看过多个不同的示例,但看不到问题所在。 I have a
.catch
after my seed_file
promise. 我的
seed_file
承诺后我有一个.catch
。
Any thoughts? 有什么想法吗?
I'm getting an Unhandled Promise Rejection error but can't figure out why
我收到未处理的承诺拒绝错误,但不知道为什么
You get this warning because you have unhandled promise rejection :). 之所以收到此警告,是因为您未处理承诺承诺 :)。 The outer
catch()
method is not handling nested promise rejections, so two options could be: 外部
catch()
方法不处理嵌套的Promise拒绝,因此有两个选择:
1) Use return on your nested promise and it will be caught from the outer catch()
: 1)对嵌套的promise使用return ,它将被外部
catch()
:
massive(config.dblink).then(db => {
app.set('db', db)
return app.get('db').seed_file().then(res => {
console.log(res)
});
}).catch(err => console.log(err) });
2) Use inner catch()
to handle differently the nested rejection: 2)使用内部
catch()
来不同地处理嵌套拒绝:
massive(config.dblink).then(db => {
app.set('db', db)
app.get('db').seed_file().then(res => {
console.log(res)
}).catch(err => console.log(err) });
}).catch(err => console.log(err) });
Demonstration: 示范:
function doPromise(someText, flag) { return new Promise(function(resolve, reject) { setTimeout(function() { flag ? resolve(someText) : reject(someText); }, 500); }); } /* The following sample demostrates unhandled rejection */ doPromise('this will resolve', true).then(function(res1) { console.log(res1); doPromise('this is unhandled promise rejection', false).then(function(res2) { console.log(res2); }); }); /* The following sample demostrates handling nested promise rejection like explained in point 1) */ doPromise('1) this will resolve', true).then(function(res1) { console.log(res1); return doPromise('1) nested rejection catched from outside', false); }).catch(err => console.log(err)); /* The following sample demostrates handling nested promise rejection like explained in point 2) */ doPromise('2) this will resolve', true).then(function(res1) { console.log(res1); doPromise('2) nested rejection catched from inside', false).catch(err => console.log(err)); }).catch(err => console.log(err));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.