如何异步返回 sequelize 实例? [英] How can I return a sequelize instance asynchronously?
问题描述
我似乎无法将 sequelize
对象从 service.js
文件正确传递到 index.js
sequelize 变量.
I can't seem to properly pass the sequelize
object from the service.js
file to the index.js
sequelize variable.
有什么地方不对劲吗?
index.js
let sequelize;
let contractModel;
(async () => {
sequelize = await lambdaHelper.init(); //The issue is here
contractModel = require('./models/Contract')(sequelize, Sequelize);
})();
service.js
exports.init = async () => {
let sequelize = await new Sequelize(process.env.FNI_DATABASE, process.env.FNI_USER, process.env.FNI_PASSWORD, {
host: process.env.FNI_HOST,
dialect: 'mysql',
operatorsAliases: false,
define: {
timestamps: false,
freezeTableName: true
},
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
});
return sequelize;
};
service2.js以下代码同步工作
exports.init = () => new Sequelize(process.env.FNI_DATABASE, process.env.FNI_USER, process.env.FNI_PASSWORD, {
host: process.env.FNI_HOST,
dialect: 'mysql',
logging: false,
operatorsAliases: false,
define: {
timestamps: false,
freezeTableName: true
},
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
});
结果
*********** 前 *********** (node:66912) UnhandledPromiseRejectionWarning: UnhandledPromiseRejectionWarning(拒绝 ID:1):类型错误:无法读取属性findAll"未定义(节点:66912)[DEP0018] 弃用警告:未处理的承诺拒绝被弃用.在未来,承诺拒绝是未处理将以非零退出终止 Node.js 进程代码.
*********** before *********** (node:66912) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'findAll' of undefined (node:66912) [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.
结果 2
*********** 前 *********** (node:66540) UnhandledPromiseRejectionWarning: 未处理的承诺拒绝(拒绝 ID:1):类型错误:无法读取属性findAll"未定义(节点:66540)[DEP0018] 弃用警告:未处理的承诺拒绝被弃用.在未来,承诺拒绝是未处理将以非零退出终止 Node.js 进程代码.正在执行(默认):SELECT 1+1 AS 结果类型错误:无法读取未定义的属性定义"在
*********** before *********** (node:66540) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'findAll' of undefined (node:66540) [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. Executing (default): SELECT 1+1 AS result TypeError: Cannot read property 'define' of undefined at module.exports at
推荐答案
await new Sequelize(...)
上面代码中期望 new Sequelize
返回一个 promise,而事实并非如此.有 authenticate
方法 这样做.
await new Sequelize(...)
in the code above expects that new Sequelize
returns a promise, while this is not so. There is authenticate
method that does that.
应该是:
exports.init = async () => {
let sequelize = new Sequelize(...);
await sequelize.authenticate();
return sequelize;
};
确保始终正确处理承诺拒绝:
Make sure promise rejections are always properly handled:
(async () => {
sequelize = await lambdaHelper.init();
contractModel = require('./models/Contract')(sequelize, Sequelize);
})().catch(console.error);
这篇关于如何异步返回 sequelize 实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!