2017-06-21 56 views
1

工作,我想不通爲什麼我不能趕上這個錯誤:燼:趕不燼數據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塊內的是被打印:

enter image description here

我猜findRecord在本地尋找首先,找到車,執行then塊和異步findRecord稍後會出現API錯誤(對於catch塊來說太晚了),也許呢?

如果是這樣的話,我該怎麼說,「在做什麼之前等待API的響應,如果響應爲空,請致電_addItemToNewCart」?

+0

我會做this.store.findRecord(...),然後(響應=> {//如果響應滿足條件,則返回新對象}); –

回答

0

找到了答案here

基本上,需要添加{ reload: true }到餘燼數據查找查詢

+0

其實你的主題看起來你想要的東西不同的答案,任何我會鼓勵更多的API文檔閱讀。那會有比導遊更詳細的信息。 https://emberjs.com/api/ ..你的情況findRecord看看https://emberjs.com/api/data/classes/DS.Store.html – kumkanillam