I am getting this error after running npm run build yield User.create({
^
在运行NPM Run Build Year User.create({^
TypeError: Cannot read properties of undefined (reading 'create')
TypeError:无法读取undefined的属性(正在读取"“)
models/index.ts
模型/索引.ts
'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
//const process = require('process');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/./config/config.js')[env];
const db: any = {};
console.log(config)
let sequelize: any;
if (config.use_env_variable) {
console.log(1)
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
console.log(2)
sequelize = new Sequelize('***', {
dialect: 'postgres',
dialectoptions: {
ssl: true
}
});
console.log(sequelize)
}
fs
.readdirSync(__dirname)
.filter((file: string) => {
return (
file.indexOf('.') !== 0 &&
file !== basename &&
file.slice(-3) === '.ts' &&
file.indexOf('.test.js') === -1
);
})
.forEach((file: any) => {
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;
export default db;
models/user.ts
Models/user.ts
'use strict';
import {
Model, UUIDV4
} from 'sequelize';
interface UserAttributes {
id: number;
first_name: string;
last_name: string;
}
module.exports = (sequelize: any, DataTypes: any) => {
class User extends Model<UserAttributes> implements UserAttributes {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
id!: number;
first_name!: string;
last_name!: string;
static associate(models: any) {
// define association here
}
}
User.init({
id: {
type: DataTypes.UUID,
defaultValue: UUIDV4,
allowNull: false,
primaryKey: true
},
first_name: DataTypes.STRING,
last_name: DataTypes.STRING
}, {
sequelize,
modelName: 'User',
});
return User;
};
index.ts
Index.ts
import express, { Application, Request, Response, NextFunction} from 'express';
const app: Application = express();
const calc = (a: number, b: number) => {
return a + b;
}
import db from './models';
db.sequelize.sync().
then(function() {
console.log('DB connection sucessful.');
}).catch((err: any)=> console.log('error has occured'));
db.User.create({
first_name: "lola",
last_name: "lola"
})
/* app.get('/', (req: Request, res: Response) => {
console.log(calc(2,5))
res.send('Hello122');
})
app.get('/hi', (req: Request, res: Response) => {
console.log(calc(2,5))
res.send('Hello122');
}) */
app.listen(5000, () => console.log('Server is running'));
I have tried importing the db in the index files with different methods but did not work. Any solution will be welcomed. Thank you
我曾尝试用不同的方法导入索引文件中的数据库,但不起作用。任何解决方案都将受到欢迎。谢谢
更多回答
Are you sure each model has a name
property? I can't find anything in the Sequelize docs referring to such a thing (but maybe I'm just bad at searching). Try adding some simple debugging? console.log("registering model", model.name)
您确定每个型号都有名称属性吗?我在续集文档中找不到任何提到这种事情的东西(但也许我只是搜索不好)。尝试添加一些简单的调试?Console.log(“注册模型”,Model.name)
Or better output db
to console prior to calling db.User.create
或者更好的做法是在调用db.User.create之前将数据库输出到控制台
我是一名优秀的程序员,十分优秀!