Ember:没有使用ember-data findRecord错误

时间:2017-06-21 18:15:46

标签: ember.js ember-data

我无法弄清楚为什么我无法捕捉到这个错误:

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消息:

enter image description here

我猜findRecord首先在本地查找,找到购物车,执行then块,异步findRecord稍后会出现API错误(对于(块)),也许?

如果是这种情况,我该如何说,"在执行任何事情之前等待API的响应,如果响应为空,请致电{{1} }" ?

1 个答案:

答案 0 :(得分:0)

找到答案here

基本上,需要将{ reload: true }添加到ember-data查询