导入 sequelize 模型说它是未定义的
Posted
技术标签:
【中文标题】导入 sequelize 模型说它是未定义的【英文标题】:Importing sequelize model says that it is undefined 【发布时间】:2022-01-21 05:11:33 【问题描述】:OBS。对不起,我的代码,我的项目就像一个游乐场,我设置它只是为了学习。 Sequelize 正在通过调用身份验证连接到数据库,但我的模型是“未定义的”,很伤心...
我将展示一张图片来展示我的项目结构。
我的索引和产品文件是这样的:
索引:
'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.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;
产品型号
'use strict';
const
Model
= require('sequelize');
module.exports = (sequelize, DataTypes) =>
class produto 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
;
produto.init(
id: type: DataTypes.NUMBER,
primaryKey: true,
titulo: DataTypes.STRING,
preco: DataTypes.NUMBER
,
sequelize,
modelName: 'produto',
);
return produto;
;
【问题讨论】:
它是“produto”,小写。 【参考方案1】:它是produto小写,半天就这样...
const produto = require('../modelos');
因为重要的是模型的名称......或类似的东西。
【讨论】:
【参考方案2】:你可以试试这个:
const Produto = require("../models/produto");
【讨论】:
以上是关于导入 sequelize 模型说它是未定义的的主要内容,如果未能解决你的问题,请参考以下文章