gpt4 book ai didi

postgresql - 使用 Sequelize CLI 生成的模型时找不到创建

转载 作者:行者123 更新时间:2023-12-03 22:32:59 27 4
gpt4 key购买 nike

我的方法如下命令所示

npx sequelize-cli model:generate --name User --attributes name:string,email:string,password:string
然后它生成了一个名为 user.js & 文件结构的模型文件,如下所示
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class User extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
User.hasMany(models.Course, {
foreignKey: 'userId'
})
}
};
User.init({
name: DataTypes.STRING
}, {
sequelize,
modelName: 'User',
});
return User;
};
此外,还创建了带有列的 Users 表。
和自动生成的 index.js 文件如下所示
'use strict';
// main model file
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
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 = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
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;
当我尝试像下面的代码一样创建用户时
import { User } from '../../../../models'

try {
await User.create({
name: 'john doe',
email: 'john@gmail.com'
})

res.json({"message": "create"})
} catch (error) {
res.send(error.message)
}
它显示

Cannot read property 'create' of undefined


我在这里做错了什么?
你能帮我解决这个问题吗?
谢谢

最佳答案

replace this code in models/index.js for printing database successfully connected or not.



    'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../../config/config.js')[env];
const db = {};

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


//for checking database successfully connected or not

sequelize
.authenticate()
.then(() => {

console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
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;



然后尝试像这样导入。

const admin = require("../../database/models").tbl_admin;

try {
await admin.create({
name: 'john doe',
email: 'john@gmail.com'
})

res.json({"message": "create"})
} catch (error) {
res.send(error.message)
}

关于postgresql - 使用 Sequelize CLI 生成的模型时找不到创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65057911/

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