[英]Nodejs Sequelize is not inserting
OKay i have litteraly been staring at this for 2 hours now and i simply cannot find the mistake: 好吧,我现在已经呆呆地盯着这个看了两个小时,而我根本找不到错误:
I have the following database table: 我有以下数据库表:
If you cannot tell it has 4 columns: 如果您不能告诉它有4列:
Using sequelize i am using the following code: 使用sequelize我正在使用以下代码:
var Competence = sequelize.define('competence', {
id: DataTypes.INTEGER,
name: DataTypes.STRING,
organization_id: DataTypes.INTEGER,
competence_type_id: DataTypes.INTEGER
}, {
freezeTableName: true,
instanceMethods: {
retrieveAll: function (org_id,onSuccess, onError) {
Location.findAll({where: {organization_id: org_id}}, {})
.ok(onSuccess).error(onError);
},
retrieveById: function (quote_id, onSuccess, onError) {
Location.find({where: {id: quote_id}}, {raw: true})
.success(onSuccess).error(onError);
},
add: function (onSuccess, onError) {
var competence = this.dataValues;
Competence.build(competence)
.save().ok(onSuccess).error(onError);
},
updateById: function (quote_id, onSuccess, onError) {
var id = quote_id;
var quotes = this.quotes;
Location.update({quotes: quotes}, {where: {id: id}})
.success(onSuccess).error(onError);
},
removeById: function (quote_id, onSuccess, onError) {
Location.destroy({where: {id: quote_id}}).success(onSuccess).error(onError);
}
}
}
),
competence_type = sequelize.define('competence_type', {
id: DataTypes.INTEGER,
name: DataTypes.STRING
});
Competence.belongsTo(competence_type,{foreignKey: 'competence_type_id'});
Notice the add function which is the one i am using to add new elements. 注意add函数,这是我用来添加新元素的函数。
This is my competence
variable: 这是我的
competence
变量:
However once i save none of my callback functions are being called not onSuccess
nor onError
但是,一旦保存,就不会再调用
onSuccess
或onError
Can anyone help me out here i am going crazy!! 任何人都可以在这里帮助我,我快要疯了!
Sequelize uses bluebird for promises. Sequelize将蓝鸟用于承诺。 So you should use
.then
and .catch
methods, not .success
( .ok
) and .error
. 所以,你应该使用
.then
和.catch
方法,而不是.success
( .ok
)和.error
。 .error
has another meaning in bluebird (see here ). .error
在bluebird中具有另一种含义(请参阅此处 )。 So for example .add
method can be written like: 因此,例如
.add
方法可以写成:
add: function (onSuccess, onError) {
var competence = this.dataValues;
Competence.build(competence).save().then(onSuccess).catch(onError);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.