我是节点的新手。
我正在尝试使用 cosign 在我的简单应用程序中添加 Sequelize。
配置/db.js
var Sequelize = require('sequelize');
var sequelize = new Sequelize('test', 'root', '', {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
}
});
module.exports = function () {
return sequelize
}
模型/user.js
var Sequelize = require('sequelize');
module.exports = function(application, req, res){
var User = sequelize.define('user', {
username: {
type: Sequelize.STRING,
}
}, {
freezeTableName: true // Model tableName will be the same as the model name
});
User.create({ username: 'fnord'})
.then(function() {
console.log('criado!');
})
}
配置/server.js
...
consign()
.include('app/routes')
.then('config/db.js')
.then('app/models')
.then('app/controllers')
.into(app);
module.exports = app;
我收到错误 sequelize is not defined´ on
var User = sequelize.define(‘user’, {`
我做错了什么?
原文由 Igor Martins 发布,翻译遵循 CC BY-SA 4.0 许可协议
在您的 moldes 文件夹中创建一个 index.js 文件,如下所示:
在你的 user.js 中做这样的事情:
http://docs.sequelizejs.com/en/1.7.0/articles/express/