gpt4 book ai didi

node.js - Sequelize 和 module.exports

转载 作者:行者123 更新时间:2023-12-03 22:17:06 24 4
gpt4 key购买 nike

我是 nodejs 和 Sequelize 的新手,并且遇到了一个我无法弄清楚如何克服的问题。我想使用我创建的连接并将其导出到模块。

像这样:

const dotEnv        = require('dotenv');
const Sequelize = require('sequelize');

dotEnv.config();

module.exports.connection = async () => {
try{
const sequelize = new Sequelize(process.env.DB_DATABASE, process.env.DB_USER, process.env.DB_PASSWORD, {
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: 'mysql',
logging: false,
define: {
charset: 'utf8',
collate: 'utf8_general_ci',
},
});
await sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(error => {
throw error;
});
}catch(error){
throw error;
}
}

然后我有另一个文件,我想在其中使用它,看起来像这样
const Sequelize     = require('sequelize');
const { connection }= require('../database');

const accountModel = connection.define('accounts', {
// attributes
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
primaryKey: true
},
name: {
type: Sequelize.STRING,
allowNull: false
},
email: {
type: Sequelize.STRING,
unique: true
},
password: {
type: Sequelize.STRING,
allowNull: false,
//is: /^[0-9a-f]{64}$/i
},
permission: {
type: Sequelize.STRING,
allowNull: false
},
discount: {
type: Sequelize.INTEGER,
allowNull: false
}
}, {
freezeTableName: true
});
module.exports = connection.model('accounts', accountModel);

问题是我被告知: TypeError: connection.define is not a function,

连接正常,数据库正在运行,其他一切正常
最后,如果我这样做,它也可以:
const dotEnv        = require('dotenv');
const Sequelize = require('sequelize');

dotEnv.config();

const sequelize = new Sequelize(process.env.DB_DATABASE, process.env.DB_USER, process.env.DB_PASSWORD, {
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: 'mysql',
logging: false,
define: {
charset: 'utf8',
collate: 'utf8_general_ci',
},
});

const accountModel = sequelize.define('accounts', {
// attributes
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
primaryKey: true
},
name: {
type: Sequelize.STRING,
allowNull: false
},
email: {
type: Sequelize.STRING,
unique: true
},
password: {
type: Sequelize.STRING,
allowNull: false,
//is: /^[0-9a-f]{64}$/i
},
permission: {
type: Sequelize.STRING,
allowNull: false
},
discount: {
type: Sequelize.INTEGER,
allowNull: false
}
}, {
freezeTableName: true
});
module.exports = sequelize.model('accounts', accountModel);

我真的不确定为什么模块一不起作用,但直接方法起作用。我曾尝试在 Google 和 Stack Overflow 上搜索解决方案。

最佳答案

第一种方法的问题是您首先导出了 async function而不是 Sequelize instance (就像你的第二个例子一样)其次,函数本身没有返回任何东西。这就是为什么没有connection.define()当您在另一个文件中需要它时起作用。

试试这个 database.js它应该工作:

module.exports.connection = new Sequelize(process.env.DB_DATABASE, process.env.DB_USER, process.env.DB_PASSWORD, {
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: 'mysql',
logging: false,
define: {
charset: 'utf8',
collate: 'utf8_general_ci',
},
});

你可以做到 authenticate()try {} catch (error) {}其他地方,例如在你的第一个 js通过要求相同的 database.js 来启动服务器的文件文件。但是对于模型定义,您只导出 new Sequelize() 很重要。实例让他们能够使用 define()

关于node.js - Sequelize 和 module.exports,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61819904/

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