最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - I'm getting an Unhandled Promise Rejection error but can't figure out why - Stack Overflow

programmeradmin2浏览0评论
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.

Any thoughts?

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.

Any thoughts?

Share Improve this question edited Oct 28, 2017 at 23:00 skirtle 29.2k4 gold badges48 silver badges62 bronze badges asked Oct 28, 2017 at 21:43 user8672583user8672583 2
  • right below console.log(res) you have an extra }). Delete that and you should be good – BigSpicyPotato Commented Oct 28, 2017 at 22:04
  • @MarkDodds this is not the case, just the code is not well formatted – codtex Commented Oct 28, 2017 at 22:50
Add a ment  | 

1 Answer 1

Reset to default 6

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:

1) Use return on your nested promise and it will be caught from the outer 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:

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));

发布评论

评论列表(0)

  1. 暂无评论