【问题标题】:how to handle HTTP errors in emberJs while using ember-data with RESTAdapters?在将 ember-data 与 RESTAdapters 一起使用时,如何处理 emberJs 中的 HTTP 错误?
【发布时间】:2014-03-12 20:37:26
【问题描述】:

我有一条路线/products。在App.ProductsRoute 中,我使用setupController 挂钩将从服务器获取的产品列表分配给本地App.Product 对象。

我将 setupController 钩子中的模型设置为:

self_controller.set('model', self_controller.store.find('product'));

这在 HTTP 状态为 200 时效果很好。但是当服务器返回一些 HTTP 错误(如 500-内部服务器错误、401-未授权错误等)时,我收到 JSON.parse 错误。我不确定如何处理 this.store.find() 调用的错误。

注意:它返回 Ember 的 promiseArray,一旦解决(在实际将其分配给模型之前)我需要检查它。对此主题的任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: rest ember.js error-handling ember-data


    【解决方案1】:

    如何使用 promise 的 catch 回调来处理错误?未经测试,但这样的东西应该可以工作:

    self_controller.store.find('product').then(function(products) {
      self_controller.set('model', products);
    }).catch(function(reason) {
      // Do something to handle the error response...
    });
    

    【讨论】:

    • 您好,我可以在出现错误时进入 catch() 函数。但问题是当我调用 some_product.save() 并收到错误时,它仍将产品存储在本地 ember-data 数组中。如何在 catch() 中撤消它..?谢谢。
    • 可能是rollback?
    • 是的。我在特定记录上使用回滚解决了它。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多