通过将模型逻辑与模型配置分离来对问题进行后续处理 [英] Sequelize Issue By Separating Model Logic from Model Configuration
问题描述
我遵循 sequelize 最佳实践,连接到主机并将所有模型导入到一个文件中,然后在与模型交互时调用该文件.出于某种原因,这似乎导致了一个问题,因为我在将 define
方法用于 sequelize 变量时出错,并且我使用包含两个逻辑的文件进行了测试,我能够添加用户.
I am following the sequelize best practices by connecting to a host and importing all models into one file and then calling that file when interacting the model. For some reason it looks like this is causing an issue as I am getting an error at using the define
method for the sequelize variable and I ran a test with a file that contained both logic together and I was able to add a user.
错误:
TypeError: Cannot read property 'define' of undefined
at new module.exports (/Users/user/Desktop/Projects/node/ann/app/models/ann-model.js:3:27)
at /Users/user/Desktop/Projects/node/ann/app/controllers/appRoutes.js:13:20
这是我的包含数据库连接的文件 (dbIndex.js):
Here is my file that contains the connection to the database (dbIndex.js):
var Sequelize = require('sequelize');
var sequelize = new Sequelize('dbname', 'user', 'pwd', {
host:'localhost',
port:'3306',
dialect: 'mysql'
});
sequelize
.authenticate()
.then(function(err) {
if (!!err) {
console.log('Unable to connect to the database:', err)
} else {
console.log('Connection has been established successfully.')
}
});
var db = {}
db.Ann = sequelize.import(__dirname + "/ann-model");
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
这是模型文件 (ann-model.js):
Here is the Model file (ann-model.js):
module.exports = function(sequelize, DataTypes) {
var Ann = sequelize.define('ann', {
ann_id: {
type: DataTypes.INTEGER,
primaryKey: true
},
ann_date: DataTypes.DATE,
}, {
freezeTableName: true
});
return Ann;
}
这里是我使用 POST 方法调用模型的地方.
Here is where I'm calling the model with a POST method.
(appRoutes.js):
(appRoutes.js):
var express = require('express');
var appRoutes = express.Router();
var Annotation = require('../models/ann-model');
appRoutes.route('/')
.get(function(req, res){
res.render('pages/activity-feed.hbs');
})
.post(function(req, res){
var ann = new Ann();
ann.ann_date = req.body.ann-date;
annotation.save(function(err){
if (err)
res.send(err);
});
});
module.exports = appRoutes;
在一个文件中结合逻辑和建模的测试文件:
Test file combining logic and modeling in one file:
var Sequelize = require('sequelize');
var sequelize = new Sequelize('dbname', 'user', 'pwd', {
host:'localhost',
port:'3306',
dialect: 'mysql'
});
var Ann = sequelize.define('ann', {
ann_id: {
type: Sequelize.INTEGER,
primaryKey: true
},
ann_date: Sequelize.DATE,
}, {
freezeTableName: true
});
sequelize.sync().then(function(){
return Ann.create({
ann_id: 3,
discovery: 'This is a test.'
});
}).then(function(tation) {
console.log(tation.get({
plain: true
}));
});
推荐答案
我认为你的 post 方法有问题,但结构真的很混乱,所以你能这样做
I think problem in your post method, but structure is really confusing so can you do this
models/index.js
"use strict";
var fs = require("fs");
var path = require("path");
var Sequelize = require("sequelize");
var env = process.env.NODE_ENV || "development";
var config = require(__dirname + '/../config/config.json')[env];
var sequelize = new Sequelize(config.database, config.username, config.password, config);
var db = {};
fs
.readdirSync(__dirname)
.filter(function(file) {
return (file.indexOf(".") !== 0) && (file !== "index.js");
})
.forEach(function(file) {
var model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(function(modelName) {
if ("associate" in db[modelName]) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
models/ann-model.js
"use strict";
module.exports = function(sequelize, DataTypes) {
var Ann = sequelize.define('ann', {
ann_id: {
type: DataTypes.INTEGER,
primaryKey: true
},
ann_date: DataTypes.DATE,
}, {
freezeTableName: true
});
return Ann;
}
routes/index.js
var express = require('express');
var appRoutes = express.Router();
var models = require('../models');
appRoutes.route('/')
.get(function(req, res){
res.send('ok');
})
.post(function(req, res){
models.ann
.build({ ann_id: 55, ann_date: new Date() })
.save()
.then(function(anotherTask) {
res.send("POST OK");
}).catch(function(error) {
res.send(error);
})
});
module.exports = appRoutes;
这篇关于通过将模型逻辑与模型配置分离来对问题进行后续处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!