2017-10-28 5 views
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}`)}) 

을 알아낼 수 없습니다 : 내가 잘못이 무엇인지 알아낼 수 없었다내가 처리되지 않은 약속 거부 오류를 받고 있어요하지만 난 다음 오류가있는 이유

(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. 

. 여러 다른 예제를 살펴 보았지만 문제를 볼 수는 없습니다. 내 seed_file 약속 후 .catch이 있습니다.

의견이 있으십니까? 당신이 처리되지 않은 약속을 거부을 가지고 있기 때문에

+0

. 그것을 삭제하면 좋을 것입니다. –

+0

@MarkDodds 그렇지 않습니다. 코드 형식이 올바르지 않습니다. – codtex

답변

4

I'm getting an Unhandled Promise Rejection error but can't figure out why

당신은 :)이 경고를 얻을. 외부 catch() 방법은 중첩 된 약속 거부 처리되지 않으므로 두 가지 옵션이 될 수있다 :

1) 사용 반환 당신의 중첩 된 약속과는 외부 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) }); 

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) }); 
: 사용은 내부 catch() 다르게 중첩 거부를 처리 할 수 ​​ 2) 6,

데모 :

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));
바로`을 console.log (고해상도)`는`)}`여분이 아래