Okay so i have a huge problem that i have no clue on how to fix. i have the following setup for my titles:
var Title = sequelize.define('title', {
id: DataTypes.INTEGER,
name: DataTypes.STRING,
organization_id: DataTypes.INTEGER
}, {
freezeTableName: true,
instanceMethods: {
retrieveAll: function (onSuccess, onError) {
Title.findAll({order: 'Rand'}, {raw: true})
.ok(onSuccess).error(onError);
},
retrieveById: function (quote_id, onSuccess, onError) {
Title.find({where: {id: quote_id}}, {raw: true})
.success(onSuccess).error(onError);
},
add: function (selectedCompetence,onSuccess, onError) {
var title = {id: null, name: this.dataValues.name, organization_id: this.dataValues.organization_id};
Title.build(this.dataValues)
.save().ok(onSuccess).error(onError);
},
updateById: function (quote_id, onSuccess, onError) {
var id = quote_id;
var quotes = this.quotes;
Title.update({quotes: quotes}, {where: {id: id}})
.success(onSuccess).error(onError);
},
removeById: function (quote_id, onSuccess, onError) {
Title.destroy({where: {id: quote_id}}).success(onSuccess).error(onError);
}
}
}
),
Title_has_competence = sequelize.define('title_has_competence', {
id: DataTypes.INTEGER,
title_id: DataTypes.INTEGER,
competence_id: DataTypes.INTEGER,
competence_level_id: DataTypes.STRING
},{
freezeTableName: true,
instanceMethods: {
retrieveAll: function (onSuccess, onError) {
Title_has_competence.findAll({order: 'Rand'}, {raw: true})
.ok(onSuccess).error(onError);
},
retrieveById: function (quote_id, onSuccess, onError) {
Title_has_competence.find({where: {id: quote_id}}, {raw: true})
.success(onSuccess).error(onError);
},
add: function (competence,onSuccess, onError) {
var ins = {id: null, title_id:competence.title_id, competence_id: competence.id, competence_level_id: competence.level};
Title_has_competence.build(ins)
.save().ok(onSuccess).error(onError);
},
updateById: function (quote_id, onSuccess, onError) {
var id = quote_id;
var quotes = this.quotes;
Title_has_competence.update({quotes: quotes}, {where: {id: id}})
.success(onSuccess).error(onError);
},
removeById: function (quote_id, onSuccess, onError) {
Title.destroy({where: {id: quote_id}}).success(onSuccess).error(onError);
}
}
});
Title.belongsToMany(Title_has_competence,{foreignKey: 'competence_id'});
In my system when you create a title you pass along an array of competence:
In this case the competences is the array: selectedCompetence Once the title has been created i need to loop through the array of selectedCompetence and insert them into the Title_has_competence table.
I feel like i have litteraly tried everything from Hooks to minor hacks but nothing has worked.
Can anyone please push me in the right direction?
Things i have attempted:
Title.build(this.dataValues)
.save().done(function(err, success, selectedCompetence){
var i = 0; // here selectedCompetence is undefined
});
Title.build(this.dataValues)
.save();
Title.hook('afterCreate', function(success,selectedCompetence){
var i = 0; // here selectedCompetence is an object with other values
})
Title.create({id: null, name: this.dataValues.name, organization_id:this.dataValues.organization_id}).then(function(title, selectedCompetence)
{
var i = 0; // once again selectedCompetence is undefined
});
Aucun commentaire:
Enregistrer un commentaire