我无法弄清楚为什么我无法捕捉到这个错误:
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);
});
},
似乎以某种方式成功解析了ember-data promise,因为正在打印then
块中的console.log消息:
我猜findRecord
首先在本地查找,找到购物车,执行then
块,异步findRecord
稍后会出现API错误(对于(块)),也许?
如果是这种情况,我该如何说,"在执行任何事情之前等待API的响应,如果响应为空,请致电{{1} }" ?