簡體   English   中英

在RESTAdapter中使用ember-data時,如何處理emberJs中的HTTP錯誤?

[英]how to handle HTTP errors in emberJs while using ember-data with RESTAdapters?

我有一條路線/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,解決后我需要檢查它(在將其實際分配給模型之前)。 對此主題的任何幫助將不勝感激。 謝謝。

使用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...
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM