简体   繁体   中英

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:

  • id
  • name
  • organization_id
  • competence_type_id

Using sequelize i am using the following code:

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.

This is my competence variable:

在此处输入图片说明

However once i save none of my callback functions are being called not onSuccess nor onError

Can anyone help me out here i am going crazy!!

Sequelize uses bluebird for promises. So you should use .then and .catch methods, not .success ( .ok ) and .error . .error has another meaning in bluebird (see here ). So for example .add method can be written like:

add: function (onSuccess, onError) {
    var competence = this.dataValues;

    Competence.build(competence).save().then(onSuccess).catch(onError);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM