heroku上的数据库与本地数据库之间的不同错误消息

时间:2019-03-14 15:47:20

标签: node.js heroku sequelize.js psql

我遇到以下问题:

我正在使用sequelize创建模型。 当我将其解构到heroku时,我得到了我用sequelize定义的自定义错误消息

var User = connection.define(
    "user",
    {
        username: {
            type: Sequelize.STRING,
            unique: {
                args: true,
                msg: "Username already taken"
            },
            allowNull: false
        },
        email: {
            type: Sequelize.STRING,
            unique: {
                args: true,
                msg: "Email already taken"
            },
            allowNull: false,
            validate: {
                isEmail: {
                    args: true,
                    msg: "Invalid email"
                }
            }
        },

当我的应用程序在heroku上运行时,当我尝试在我的错误处理“ try catch block”中创建具有相同用户名的用户时,收到此错误消息“ Username hastaken” 但是,当我在本地计算机上尝试此操作时,此自定义错误消息不起作用。

最小示例代码:

try {
  var user = await sequelize.models.user.create(args);
} catch (error) {
    console.log("createUser error:", error);            
}

当我在本地运行代码时,我从此console.log()获得以下输出:

createUser error: { SequelizeUniqueConstraintError: doppelter Schlüsselwert verletzt Unique-Constraint »users_username_key«
at Query.formatError (/home/whatever/NodeApps/MyStuff/testapp/node_modules/sequelize/lib/dialects/postgres/query.js:328:16)
at query.catch.err (/home/whatever/NodeApps/MyStuff/testapp/node_modules/sequelize/lib/dialects/postgres/query.js:86:18)
at tryCatcher (/home/whatever/NodeApps/MyStuff/testapp/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/whatever/NodeApps/MyStuff/testapp/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/home/whatever/NodeApps/MyStuff/testapp/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/home/whatever/NodeApps/MyStuff/testapp/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/home/whatever/NodeApps/MyStuff/testapp/node_modules/bluebird/js/release/promise.js:690:18)
at _drainQueueStep (/home/whatever/NodeApps/MyStuff/testapp/node_modules/bluebird/js/release/async.js:138:12)
at _drainQueue (/home/whatever/NodeApps/MyStuff/testapp/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/home/whatever/NodeApps/MyStuff/testapp/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues [as _onImmediate] (/home/whatever/NodeApps/MyStuff/testapp/node_modules/bluebird/js/release/async.js:17:14)
at processImmediate (timers.js:637:19)
name:'SequelizeUniqueConstraintError',
errors: {},
fields: undefined,

在heroku上我得到了一个:

createUser error: { SequelizeUniqueConstraintError: Username already taken
at Query.formatError (/app/node_modules/sequelize/lib/dialects/postgres/query.js:325:18)
at query.catch.err (/app/node_modules/sequelize/lib/dialects/postgres/query.js:86:18)
at tryCatcher (/app/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/app/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/app/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/app/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/app/node_modules/bluebird/js/release/promise.js:690:18)
at _drainQueueStep (/app/node_modules/bluebird/js/release/async.js:138:12)
at _drainQueue (/app/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/app/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues [as _onImmediate] (/app/node_modules/bluebird/js/release/async.js:17:14)
at processImmediate (timers.js:637:19)
name: 'SequelizeUniqueConstraintError',
errors:
[ ValidationErrorItem {
message: 'Username already taken',
type: 'unique violation',
path: 'username',
value: 'testuser',
origin: 'DB',
instance: [user],
validatorKey: 'not_unique',
validatorName: null,
validatorArgs: [] } ],
fields: { username: 'testuser' },

因此,相比之下,错误对象为,而字段未定义

我只是再次将我所有的代码提交给heroku,以确保我的本地代码与在heroku上运行的代码匹配。

现在,我想知道为什么自定义消息不适用于本地数据库。如果您需要更多信息,请告诉我:)

1 个答案:

答案 0 :(得分:0)

从两次回溯中行号的不同来看,听起来您在本地和Heroku中sequelize库的版本不同:

在本地,您会在以下位置遇到失败:

...sequelize/lib/dialects/postgres/query.js:328:18

在Heroku中,位于:

...sequelize/lib/dialects/postgres/query.js:325:18

这表明这些库的版本不同-您可以考虑尝试在本地进行更新,或者以某种方式确保在两个地方都安装相同的版本。