【发布时间】:2015-10-25 15:48:29
【问题描述】:
当我尝试使用包含模型中的属性时收到超时错误,我想知道这是否与我尝试包含该属性的方式有关,或者与我的代码 sn-p 无关的问题。
这是我的路线查询(db.Description,body 就是我所说的):
router.get('/:pattern/:color/result', function(req, res, image){
console.log(req.params.color);
console.log(req.params.pattern);
db.Images.findAll({
where: {
pattern: req.params.pattern,
color: req.params.color
},
include: [{model: db.Description, attributes: 'body'}],
attributes: ['id', 'pattern', 'color', 'imageUrl', 'imageSource', 'description_id', 'description.body']
}).then(function(image){
//console.log(doc.descriptions_id);
res.render('pages/suit-result.hbs', {
pattern : req.params.pattern,
color : req.params.color,
image : image
})
});
});
这是我的图像模型:
module.exports = function(sequelize, DataTypes){
var Images = sequelize.define('images', {
pattern: DataTypes.STRING,
color: DataTypes.STRING,
imageUrl: DataTypes.STRING,
imageSource: DataTypes.STRING,
description_id: DataTypes.INTEGER
}, {
classMethods: {
associate: function(db) {
Images.belongsTo(db.Description, {foreignKey: 'description_id'});
}
}
});
return Images;
}
这是我的描述模型:
module.exports = function(sequelize, DataTypes) {
var Description = sequelize.define('description', {
description_id: {
type: DataTypes.INTEGER,
primaryKey: true
},
color: DataTypes.STRING,
body: DataTypes.STRING
}, {
freezeTableName: true
});
return Description;
}
这是我加入的地方:
var Sequelize = require('sequelize');
var sequelize = new Sequelize("assistant", "admin", "pwd", {
host: "site.com",
port: 3306,
dialect: 'mysql'
});
var db = {};
db.Description = sequelize.import(__dirname + "/descriptionModel");
db.Images = sequelize.import(__dirname + "/imagesModel");
db.Images.associate(db);
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
【问题讨论】:
-
属性选项应该是数组或对象 (sequelize.readthedocs.org/en/latest/api/model/…)。如果这导致错误,您可能会从客户端看到超时错误。但是,如果您正在查看服务器是否有错误,我不确定是否是这样。你能看到服务器端错误吗?如果是这样,他们怎么说?
-
@DJStroky 只是将属性放入数组中。谢谢!
标签: mysql node.js express sequelize.js