2016-11-28 41 views
0

在模型上調用.create()時出現以下錯誤。水線錯誤:「未知規則:默認」

型號:

attributes: { 
    user : { 
     type: 'integer' 
    }, 
    number : { 
     type: 'string' 
    } 
    } 

電話:

sails.models.phone.create({ 
    user: 2, 
    number: '5555555555', 
    updated_at: Sun Nov 27 2016 16:59:45 GMT-0800 (PST) 
}); 

錯誤:

Unknown rule: default 
    at Object.matchRule (/Developer/repos/bond/api/node_modules/waterline/node_modules/anchor/lib/match/matchRule.js:38:11) 
    at Anchor.to (/Developer/repos/bond/api/node_modules/waterline/node_modules/anchor/index.js:106:48) 
    at afterwards (/Developer/repos/bond/api/node_modules/waterline/lib/waterline/core/validations.js:229:41) 
    at /Developer/repos/bond/api/node_modules/async/lib/async.js:52:16 
    at Object.async.forEachOf.async.eachOf (/Developer/repos/bond/api/node_modules/async/lib/async.js:236:30) 
    at Object.async.forEach.async.each (/Developer/repos/bond/api/node_modules/async/lib/async.js:209:22) 
    at _eachValidation (/Developer/repos/bond/api/node_modules/waterline/lib/waterline/core/validations.js:189:11) 
    at /Developer/repos/bond/api/node_modules/async/lib/async.js:181:20 
    at Object.async.forEachOf.async.eachOf (/Developer/repos/bond/api/node_modules/async/lib/async.js:233:13) 
    at Object.async.forEach.async.each (/Developer/repos/bond/api/node_modules/async/lib/async.js:209:22) 
    at Validator.validate (/Developer/repos/bond/api/node_modules/waterline/lib/waterline/core/validations.js:144:9) 
    at /Developer/repos/bond/api/node_modules/waterline/lib/waterline/query/validate.js:42:25 
    at /Developer/repos/bond/api/node_modules/async/lib/async.js:718:13 
    at Immediate.iterate [as _onImmediate] (/Developer/repos/bond/api/node_modules/async/lib/async.js:262:13) 
    at processImmediate [as _immediateCallback] (timers.js:383:17) 

我該如何解決這個問題?

我查看了similar errors,但是我的型號都沒有default屬性,包括上面顯示的phone型號。爲什麼會發生這種情況,我該如何解決?


解決方案

所以事實證明,中.update()吃水線調用修改了輸入信息。我寫了一個函數來創建或更新水線模型,當他們修正auto updated_at issue時,他們必須修改傳入的信息。

我以前的代碼沒有工作:

createOrUpdate: function(model, primary, data){ 
    return model.update(primary, data) 
    .then(function updateCB(updated){ 
    if (updated.length == 0){ 
     return model.create(data) //data here had an added updated_at field 
     .then(function(created){ 
     return created; 
     }); 
    } 

    if (updated.length > 0){ 
     updated = updated[0]; 
    } 
    return updated; 
    }); 
} 

我的新代碼,做工作:

createOrUpdate: function(model, primary, data){ 
    const data1 = JSON.parse(JSON.stringify(data)); // deep copy 
    const data2 = JSON.parse(JSON.stringify(data)); // deep copy 
    return model.update(primary, data1) 
    .then(function updateCB(updated){ 
    if (updated.length == 0){ 
     return model.create(data2) 
     .then(function(created){ 
     return created; 
     }); 
    } 

    if (updated.length > 0){ 
     updated = updated[0]; 
    } 
    return updated; 
    }); 
} 
+0

創建後的列名是'updatedAt'。 – khushalbokadey

回答

2

不能調用更新:

updated_at: Sun Nov 27 2016 16:59:45 GMT-0800 (PST) 

的updated_at是自動填充字段。從請求中刪除它(打電話給你),它會起作用。

+0

感謝您的支持!我其實並沒有手動與該字段通話,所以我沒有注意到它。現在修好了! – smileham

+0

看到我添加的解決方案,爲什麼我很困惑,因爲這已經工作了幾個月。我會帶着Waterline團隊提出這個問題。 – smileham