Sequelize文档更新

时间:2015-12-06 22:34:53

标签: javascript node.js sequelize.js

我正在尝试使用sequelize为node.js创建一个简单的crud更新函数所有文档都给出了类似的示例,这些示例都会导致相同的错误

Error on update:  { dataValues: 
{ sku: 'BBB123',
 qty_on_hand: 3,
 trigger_qty: 4,
 replenish_qty: 5,
 createdAt: Sun Dec 06 2015 15:31:08 GMT-0500 (EST),
 updatedAt: Sun Dec 06 2015 15:31:08 GMT-0500 (EST) },
_previousDataValues: 
{ sku: 'BBB123',
 qty_on_hand: 3,
 trigger_qty: 4,
 replenish_qty: 5,
 createdAt: Sun Dec 06 2015 15:31:08 GMT-0500 (EST),
 updatedAt: Sun Dec 06 2015 15:31:08 GMT-0500 (EST) },
_changed: {},
'$modelOptions': 
 { timestamps: true,
 instanceMethods: {},
 classMethods: {},
 validate: {},
 freezeTableName: false,
 underscored: false,
 underscoredAll: false,
 paranoid: false,
 whereCollection: { sku: 'BBB123' },
 schema: null,
 schemaDelimiter: '',
 defaultScope: null,
 scopes: [],
 hooks: {},
 indexes: [],
 name: { plural: 'units', singular: 'unit' },
 omitNull: false,
 sequelize: 
  { options: [Object],
    config: [Object],
    dialect: [Object],
    models: [Object],
    modelManager: [Object],
    connectionManager: [Object],
    importCache: {},
    test: [Object],
    queryInterface: [Object] },
 uniqueKeys: {},
 hasPrimaryKeys: true },
'$options': 
 { isNewRecord: false,
 '$schema': null,
 '$schemaDelimiter': '',
 raw: true,
 attributes: 
  [ 'sku',
    'qty_on_hand',
    'trigger_qty',
    'replenish_qty',
    'createdAt',
    'updatedAt' ] },
hasPrimaryKeys: true,
__eagerlyLoadedAssociations: [],
isNewRecord: false }

我的代码如下:

// Update One units
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
_update = function(data,success,fail){
    // var cleanData = data.sanitize(item);
    // if(!cleanData) return false;
    unit.find({where:{sku:data.sku}}).then(function (err, data) {
        if(err){
            console.log("Error on update: ", err);
        }
        if(data){
            data.updateAttributes({
                qty_on_hand:20
            }).success(function (data) {
                console.log("Success on update: ", data);
            })
        }
    });
}
 _update({sku:"BBB123"});

有人可以告诉我我做错了吗?

1 个答案:

答案 0 :(得分:1)

了解承诺!您没有收到错误,但实际结果是错误的!

您希望捕获承诺上的错误,使用struct at { int element = 5; // <---- Illegal in C struct at *next; } (promise方法,而不是关键字)来执行此操作。

您应该重新构建代码:

.catch

Sequelize使用bluebird的承诺,你可能想看看their documentation,它非常强大。