Handling promise rejection in ember-data with findQuery()

2.1k views Asked by At

I cannot seem to get ember-data to reject failed (404's) when using findQuery(..query..); find(..id..); works fine.

So in the route:

App.PostRoute = Ember.Route.extend({

  serialize: function(model, params) {
    return { post_id: model.get('slug') };
  },

  model: function(params){
    var query = {};
    query.slugs = params.post_id;
    return App.Post.findQuery(query).then( 
      function (data) {
        return data.get('firstObject');
      }, 
      function (error) {
        console.log('error');
        throw 'boom!';
      }
    )
  },

  setupController: function(controller, model){
    this.controllerFor('post').set('content', model);
  },

  events: {
    error: function (reason, transition) {
     console.log('Error!');
    }
  }

});

I have also tried this:

return App.Post.findQuery(query).then( function (data) {
  return data.get('firstObject');
}).then( null, function (error) {
  console.log('error');
  throw 'boom!';
});

No joy. I can see the request to the URL returning as 404, but the promises error is never triggered. What am I missing?

1

There are 1 answers

3
intuitivepixel On

I don't know if it's only a typo, but the events hash in your PostRoute should be called events you seam to have defined it as singular event this might be the problem why your error hook inside that hash not getting found and this not invoked:

App.PostRoute = Ember.Route.extend({
  ...
  events: {
    error: function (reason, transition) {
      console.log('Error!');
    }
  }
});

Hope it helps.