I can't figure out why I can't catch this error:
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
This is happening when trying to add an item to my cart service, but there's a problem with the API. Specifically, I am testing a security scenario where there's a token mismatch with the API. The API will send back an empty response, which it is doing, but I want ember to catch
that error and trigger a addItemToNewCart
function.
This is the 'add' method:
// 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);
});
},
It appears that somehow the ember-data promise is successfully resolving, because the console.log messages I have within the then
block are being printed:
I am guessing findRecord
is looking locally first, finding the cart, executing the then
block and an async findRecord
is coming in later with an error from the API (too late for the catch block), maybe?
If that's the case, how do I say, "wait for a response from the API before doing anything and if the response is empty, call _addItemToNewCart
" ?
Aucun commentaire:
Enregistrer un commentaire