尝试捕获不捕获承诺错误

时间:2019-11-01 12:16:47

标签: node.js

以下是我的代码

try {
    const connect_to_database = async () =>{
        const sequelize = new Sequelize('testing','sugumar','mypassward',{
            host:'127.0.0.1',
            dialect:'mysql'
        })
        await sequelize.authenticate()
    }
    connect_to_database()
} catch(e) {

}

我不想抛出任何错误,但是它会在终端中抛出错误

部分错误是

(node:25701) UnhandledPromiseRejectionWarning: SequelizeAccessDeniedError: Access denied for user 'sugumar'@'localhost' (using password: YES)
at Promise.tap.then.catch.err (/home/user/Desktop/sugumar/testing/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:125:19)
at tryCatcher (/home/user/Desktop/sugumar/testing/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/user/Desktop/sugumar/testing/node_modules/bluebird/js/release/promise.js:547:31)

4 个答案:

答案 0 :(得分:1)

您必须等待像await connect_to_database()这样的promise函数调用。

connect_to_database函数是async,表示它返回一个promise

答案 1 :(得分:0)

您可以尝试使用此代码,看看它是否可以像我目前仅在Sequelize上工作一样。

const sequelize=new Sequelize('database','root','password',{host:'localhost',dialect:'mysql',logging:false});
sequelize.authenticate().then(function(err) 
{
  console.log('Connection has been established successfully.');
}).catch(function(err)
{

});

答案 2 :(得分:0)

const connect_to_database = async () => {
  try {
    const sequelize = new Sequelize('testing', 'sugumar', 'mypassward', {
      host: '127.0.0.1',
      dialect: 'mysql'
    })
    await sequelize.authenticate();
  } catch (error) {

  }
}

connect_to_database();

答案 3 :(得分:0)

我应该只将try catch用于等待呼叫