gpt4 book ai didi

node.js - Sequelize db.define 不是函数吗?

转载 作者:行者123 更新时间:2023-12-03 22:36:18 25 4
gpt4 key购买 nike

我的 Sequelize 有问题。我阅读了文档并和他们一样做,但总是遇到以下错误。请帮我解决这个错误。
这是错误

TypeError: db.define is not a functionat Object. (/home/robin/Public/server/models/users.js:6:17)

at Module._compile (internal/modules/cjs/loader.js:1151:30)

at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)

at Module.load (internal/modules/cjs/loader.js:1000:32)

at Function.Module._load (internal/modules/cjs/loader.js:899:14)

at Module.require (internal/modules/cjs/loader.js:1040:19)

at require (internal/modules/cjs/helpers.js:72:18)

at Object. (/home/robin/Public/server/routes/user.js:4:14)

at Module._compile (internal/modules/cjs/loader.js:1151:30)

at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)

at Module.load (internal/modules/cjs/loader.js:1000:32)

at Function.Module._load (internal/modules/cjs/loader.js:899:14)

at Module.require (internal/modules/cjs/loader.js:1040:19)

at require (internal/modules/cjs/helpers.js:72:18)

at Object. (/home/robin/Public/server/app.js:10:19)

at Module._compile (internal/modules/cjs/loader.js:1151:30)

[nodemon] app crashed - waiting for file changes before starting...


这是代码
模型
const Sequelize = require("sequelizer");
const db = require("../config/database");

const users = db.define("user", {
email: {
type: Sequelize.STRING
}
});

module.exports = users;
数据库
var fs = require("fs");
var path = require("path");
var Sequelize = require("sequelize");
var basename = path.basename(module.filename);
var env = process.env.NODE_ENV || "development";
var config = require(__dirname + "/../config/config.json")[env];
var db = {};

if (config.use_env_variable) {
var sequelize = new Sequelize(process.env[config.use_env_variable]);
} else {
var sequelize = new Sequelize(
config.database,
config.username,
config.password,
config
);
}

fs.readdirSync(__dirname)
.filter(function(file) {
return (
file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js"
);
})
.forEach(function(file) {
var model = sequelize["import"](path.join(__dirname, file));
db[model.name] = model;
});

Object.keys(db).forEach(function(modelName) {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

sequelize
.authenticate()
.then(() => {
console.log("Connection has been established successfully.");
})
.catch(err => {
console.error("Unable to connect to the database:", err);
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

最佳答案

我遇到了同样的问题,经过一些研究,似乎在使用 sequelize-cli 时,生成的 models/index.js 文件与所有 nodejs 版本都不兼容。

我发现解决此问题的解决方法是将 index.js 代码调整为:

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('../config/config.json')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable]);
} else {
sequelize = new Sequelize(
config.database, config.username, config.password, config
);
}

fs
.readdirSync(__dirname)
.filter(file =>
(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);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

希望这可以帮助!

关于node.js - Sequelize db.define 不是函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60014953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com