Catch 404 error when using store.findQuery

六眼飞鱼酱① 提交于 2020-01-24 17:00:08

问题


I'm using Embers findQuery method and wonder how to catch 404 errors when there are no results?

this.store.findQuery('customer', { hasProjects: true, getArchivedProjects: archived }).then(function(customers) {
});

If the query is empty, the code inside this then function doesn't get fired, so I can't even check the type of customers.

Example:

this.store.findQuery('customer', { hasProjects: true, getArchivedProjects: archived }).then(function(customers) {
  console.log('foo')
});

If the query returns a 404, console.log doesn't be fired.


回答1:


The findQuery function returns a promise. You may then provide two functions to the then(), the first being the success path, the second being the failure path... for example:

this.store.findQuery('customer', { hasProjects: true, getArchivedProjects: archived }).then(function(customers) {
    console.log('foo')
}, function(error) { /* do something with error */ });



回答2:


Alternative answer:

Add a error hook to the corresponding route:

App.CustomersIndexRoute = Ember.Route.extend({
  actions: {
    error: function(reason) {
      if (reason.status === 404) {
        // do something ...
      }
    }
  }
})

See: http://emberjs.com/guides/routing/asynchronous-routing/#toc_when-promises-reject.



来源:https://stackoverflow.com/questions/20905772/catch-404-error-when-using-store-findquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!