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

Sca*_*les 3 javascript express massive

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}`)})
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

(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.
Run Code Online (Sandbox Code Playgroud)

我一直无法弄清楚出了什么问题。我查看了多个不同的示例,但看不到问题所在。我有一个.catch在我的seed_file承诺之后。

有什么想法吗?

cod*_*tex 6

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

您收到此警告是因为您有未处理的承诺拒绝:)。外部catch()方法不处理嵌套的承诺拒绝,因此两个选项可能是:

1)对嵌套的承诺使用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) });
Run Code Online (Sandbox Code Playgroud)

2)使用innercatch()对嵌套拒绝进行不同处理:

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) });
Run Code Online (Sandbox Code Playgroud)

示范:

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) });
Run Code Online (Sandbox Code Playgroud)