简体   繁体   English

我收到未处理的承诺拒绝错误,但不知道为什么

[英]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.

相关问题 我不知道为什么我在这里得到“无法读取未定义的属性'长度'的错误”错误 - I can't figure out why I'm getting the “Cannot read property 'length' of undefined” error here 无法弄清楚为什么我得到“无法读取未定义的属性'0'”错误 - Can't figure out why I'm Getting “Cannot read property '0' of undefined” Error 尝试使用 puppeteer 插件,但我不断收到 Unhandled Promise Rejection: ReferenceError: Can't find variable: require - Trying to use puppeteer plugin but I keep getting an error for Unhandled Promise Rejection: ReferenceError: Can't find variable: require 未处理的承诺拒绝错误:发送标头后无法设置标头 - Unhandled promise rejection Error: Can't set headers after they are sent 我该如何解决这个未处理的 promise 拒绝错误 - how can i fix this error of unhandled promise rejection 我不断收到未处理的承诺拒绝 - i keep getting unhandled promise rejection 为什么这是未处理的承诺拒绝? - Why is this an Unhandled Promise Rejection? 为什么未处理的承诺拒绝 - Why unhandled promise rejection 为什么会收到UnhandledPromiseRejectionWarning:未处理的承诺被拒绝? - Why I get UnhandledPromiseRejectionWarning: Unhandled promise rejection? 我不明白为什么我会无限循环 - I can't figure out why I'm getting infinite loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM