工作,我想不通爲什麼我不能趕上這個錯誤:燼:趕不燼數據findRecord錯誤
Assertion Failed: You made a 'findRecord' request for a 'cart' with id '3ea1901a-56a9-11e7-a8f4-60e147bfe84c', but the adapter's response did not have any data
這是試圖將項目添加到我的購物車服務時發生的事情,但是API存在問題。具體而言,我正在測試一個安全方案,其中存在令牌與API不匹配。該API將發回一個空的響應,它正在做,但我想燼catch
該錯誤並觸發一個addItemToNewCart
函數。
這是「添加」方法:
// cart methods
add(item) {
let self = this;
// get cart from API first. if this fails (in catch clause) a new one should be created
return this.get('store').findRecord('cart', get(this, 'cartObj.id')).then(response => {
console.log("RESPONSE", response.get('cartitems'));
// check if cart already has lineitem
let existingLineItem = response.get('cartitems').findBy('skuid', item.skuid);
if (existingLineItem) { // if item is already in cart, just add more
console.log("line item in response, adding quantity");
set(existingLineItem, 'quantity', parseInt(existingLineItem.quantity)+parseInt(item.quantity))
} else {
console.log("line item not in response, adding new");
response.get('cartitems').addObject(item);
}
// saving persists the cart back to API
response.save().then(newCart => {
set(this, 'cartObj', newCart);
});
}).catch(e => {
// this is not firing even though there is an error
console.log("problem with findRecord - create a new cart and add item to it", e.message);
self._addItemToNewCart(item);
});
},
似乎不知何故餘燼數據承諾被成功解決,這是因爲消息的console.log我有then
塊內的是被打印:
我猜findRecord
在本地尋找首先,找到車,執行then
塊和異步findRecord
稍後會出現API錯誤(對於catch塊來說太晚了),也許呢?
如果是這樣的話,我該怎麼說,「在做什麼之前等待API的響應,如果響應爲空,請致電_addItemToNewCart
」?
我會做this.store.findRecord(...),然後(響應=> {//如果響應滿足條件,則返回新對象}); –