在 Sequelize 中使用 POST 方法
Using the POST method with Sequelize
我正在尝试调试我似乎无法根据用户在表单字段中输入的值为我的对象创建新记录的情况。现在,我正忙于 post 无法识别 .create
的对象创建方面。我还应该使用 .create
还是 .build
?我都试过了,都发生了同样的错误。
TypeError: Ann.create is not a function
at /Users/user/Desktop/Projects/node/nodeapp/app/controllers/appRoutes.js:13:20
appRoutes.js:
var express = require('express');
var appRoutes = express.Router();
var Annotation = require('../models/annotation-model');
appRoutes.route('/')
.get(function(req, res){
res.render('pages/activity-feed.hbs');
})
.post(function(req, res){
var annotation = new Annotation.create({
annotation_date: req.body.annotation-date,
}).then(function(user){
console.log(user.get({plain:true}))
});
annotation.save(function(err){
if (err)
res.send(err);
});
});
module.exports = appRoutes;
控制器索引(dbIndex.js):
var Sequelize = require('sequelize');
var sequelize = new Sequelize('test', 'admin', '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.Annotation = sequelize.import(__dirname + "/annotation-model");
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
注解-model.js:
module.exports = function(sequelize, DataTypes) {
var Annotation = sequelize.define('table', {
annotation_id: {
type: DataTypes.INTEGER,
primaryKey: true
},
annotation_date: DataTypes.DATE,
}, {
freezeTableName: true
});
return Annotation;
}
POST 方法的表格 (activity-feed.hbs):
<div class="row">
<div class="col-md-6 col-md-offset-3" style="background-color:#ffe680;">
<div class="annotation-form">
<img class="media-object" src="http://placehold.it/80x80" alt="Generic placeholder image">
<form action="/app" method="post">
<label for="annotation-date">Annotation Date:</label>
<br />
<button>Create</button>
</form>
</div>
</div>
</div>
之所以得到 Ann.create is not a function
,是因为如果您遵循此结构 everything will be ok, see models/index.js
and routes/index.js
plus read here
,则没有正确引用您的 annotation-model.js:
这个问题的解决方案分为两部分。这两部分都可以在 appRoutes.js
文件中找到。
var models
应该指向 dbIndex.js
文件,而不是模型定义的位置 (annotation-model.js
)。 var models = require('../models/dbIndex);
必须在dbIndex
中指定对象,这样我们才能使用.create
方法。这意味着 models.Annotation.create()
我正在尝试调试我似乎无法根据用户在表单字段中输入的值为我的对象创建新记录的情况。现在,我正忙于 post 无法识别 .create
的对象创建方面。我还应该使用 .create
还是 .build
?我都试过了,都发生了同样的错误。
TypeError: Ann.create is not a function
at /Users/user/Desktop/Projects/node/nodeapp/app/controllers/appRoutes.js:13:20
appRoutes.js:
var express = require('express');
var appRoutes = express.Router();
var Annotation = require('../models/annotation-model');
appRoutes.route('/')
.get(function(req, res){
res.render('pages/activity-feed.hbs');
})
.post(function(req, res){
var annotation = new Annotation.create({
annotation_date: req.body.annotation-date,
}).then(function(user){
console.log(user.get({plain:true}))
});
annotation.save(function(err){
if (err)
res.send(err);
});
});
module.exports = appRoutes;
控制器索引(dbIndex.js):
var Sequelize = require('sequelize');
var sequelize = new Sequelize('test', 'admin', '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.Annotation = sequelize.import(__dirname + "/annotation-model");
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
注解-model.js:
module.exports = function(sequelize, DataTypes) {
var Annotation = sequelize.define('table', {
annotation_id: {
type: DataTypes.INTEGER,
primaryKey: true
},
annotation_date: DataTypes.DATE,
}, {
freezeTableName: true
});
return Annotation;
}
POST 方法的表格 (activity-feed.hbs):
<div class="row">
<div class="col-md-6 col-md-offset-3" style="background-color:#ffe680;">
<div class="annotation-form">
<img class="media-object" src="http://placehold.it/80x80" alt="Generic placeholder image">
<form action="/app" method="post">
<label for="annotation-date">Annotation Date:</label>
<br />
<button>Create</button>
</form>
</div>
</div>
</div>
之所以得到 Ann.create is not a function
,是因为如果您遵循此结构 models/index.js
and routes/index.js
plus read here
annotation-model.js:
这个问题的解决方案分为两部分。这两部分都可以在 appRoutes.js
文件中找到。
var models
应该指向dbIndex.js
文件,而不是模型定义的位置 (annotation-model.js
)。var models = require('../models/dbIndex);
必须在
dbIndex
中指定对象,这样我们才能使用.create
方法。这意味着models.Annotation.create()