2015-12-06 81 views
-1

我試圖創建使用sequelize用於node.js的所有文檔CRUD一個簡單的更新功能給了我類似的例子,所有導致同樣的錯誤Sequelize文檔更新

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

閱讀承諾!你不會得到一個錯誤,但實際結果!

您是否想要發現承諾錯誤,請使用.catch(承諾方法,而不是關鍵字)。

你應該調整你這樣的代碼:

_update = function(data,success,fail){ 
    unit.find({where:{sku:data.sku}}) 
     .then(function (data) { 
     if(data){ 
      data.updateAttributes({ 
       qty_on_hand:20 
      }).success(function (data) { 
       console.log("Success on update: ", data); 
      }) 
     } 
     }).catch(function(err){ 
     console.trace(err). 
     }); 
} 

Sequelize使用藍鳥的承諾,你可能想看看their documentation,這是相當強大的。