我得到了一个包含所有序列化模式模型的模型文件夹,并试图在此模型文件夹中编写index.js;但是当我运行该应用程序时,index.js中出现错误“未定义续集”。请在下面查看我的代码。
"use strict";
const fs = require("fs");
const path = require("path");
const Sequelize = require("sequelize");
const basename = path.basename(module.filename);
const env = process.env.NODE_ENV || "development";
const config = require(__dirname + "/../config/config.json")[env];
const db = {};
if (config.use_env_variable) {
const sequelize = new Sequelize(process.env[config.use_env_variable]);
} else {
const sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs
.readdirSync(__dirname)
.filter((file) => {
return (file.indexOf(".") !== 0) && (file !== basename) && (file.slice(-3) === ".js");
})
.forEach((file) => {
const model = sequelize["import"](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach((modelName) => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
sequelize
.authenticate()
.then(() => {
console.log("Connection has been established succesfully");
})
.catch((err) => {
console.log("Unable to connect to the database:", err);
})
db.sequelize = sequelize;
db.Sequelize = Sequelize;
db.group = require('./group')(sequelize, Sequelize);
db.service = require('./service')(sequelize, Sequelize);
db.store = require('./store')(sequelize, Sequelize);
db.appointment = require('./appointment')(sequelize, Sequelize);
module.exports = db;
答案 0 :(得分:1)
您已在if块内部声明了“ sequelize”。这就是为什么您会收到该错误。希望对您有帮助。