我遵循sequelize最佳实践,通过连接到主机并将所有模型导入一个文件,然后在交互模型时调用该文件。出于某种原因,看起来这会导致问题,因为我在使用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;
方法获取sequelize变量时遇到错误,并且我使用包含两个逻辑的文件运行测试并且我能够添加一个用户。
错误:
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;
}
这是我的文件,其中包含与数据库的连接(dbIndex.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;
这是模型文件(ann-model.js):
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方法调用模型的地方。
(appRoutes.js):
{{1}}
将逻辑和建模结合在一个文件中的测试文件:
{{1}}
答案 0 :(得分:1)
我认为你的帖子方法存在问题,但结构真的很混乱所以你能做到这一点
<强>模型/ 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;
<强>模型/ 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;
}
<强>路由/ 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;