【问题标题】:Receiving Error when I use Sequelize on Ubuntu 18.04. No errors happen with the same exact files on Mac在 Ubuntu 18.04 上使用 Sequelize 时收到错误。 Mac 上完全相同的文件不会发生错误
【发布时间】:2019-09-25 02:34:17
【问题描述】:

我正在使用带有 ATOM 的 ubuntu 18.04 笔记本电脑。当我尝试使用 sequelizer 时出现此错误。

UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在 asyn 内部抛出 c 函数没有 catch 块,或者通过拒绝未使用 .catch() 处理的承诺。 (拒绝编号:1) (节点:16986)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。在未来,承诺拒绝不是 处理后的 Node.js 进程会以非零退出代码终止。

这是在我的 models.js 文件中:

const Sequelize = require('sequelize');

// Connect to the database
const sequelize = new Sequelize({
  database: 'sequelize_lesson',
  dialect:  'postgres',
});


// Define an Artist
const Artist = sequelize.define('artist', {
  name: Sequelize.STRING,
});


// Define a Record
const Record = sequelize.define('record', {
  title:           Sequelize.STRING,
  year:            Sequelize.INTEGER,
  cover_image_url: Sequelize.STRING,
});

// define the association between Artists --<< Records
// @see https://www.postgresql.org/docs/10/static/ddl-constraints.html#DDL-CONSTRAINTS-FK
Artist.hasMany(Record, { onDelete: 'cascade' });

// make the association from both directions
Record.belongsTo(Artist);

module.exports = {
  Artist,
  Record,
  sequelize,
};

and in the file I am trying to run resetDb.js

const { sequelize } = require('./models');

const main = async () => {
  await sequelize.sync({ force: true });
  process.exit();
};

main();

//this is what I am trying to run with the command
node resetDb.js

【问题讨论】:

    标签: javascript node.js sequelize.js


    【解决方案1】:

    它告诉您需要使用try/catch.catch(),因为Promise 中抛出了异常。可能只是因为你有process.exit() 而不是process.exit(0)?在任何一种情况下,使用类似这样的东西来查看错误:

    const main = async () => {
      try {
        await sequelize.sync({ force: true });
        process.exit(0); // exit code 0 is normal
      } catch (err) {
        // this will show the error
        console.log('There was an error!', err);
      }
    };
    

    【讨论】:

      猜你喜欢
      • 2017-07-03
      • 1970-01-01
      • 2021-09-18
      • 2021-01-11
      • 2020-04-17
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 1970-01-01
      相关资源
      最近更新 更多