Ember.js - How to handle error with DS.store.findR

2019-07-08 10:00发布

问题:

I am using following simple code to retrieve user from server.

var someUser = this.store.findRecord('user', 0);

I am using this for retrieving the user. if user is not found on 0 id,

server returns 404. and error as per json api.

but how do i know about error on client side about it ?

回答1:

Taken from Ember guides:

Use store.findRecord() to retrieve a record by its type and ID. This will return a promise that fulfills with the requested record.

Since the return value is a promise, you can use it as any other promise:

this.store.findRecord('user', 0)
  .then(function(user){
    // user has been found
    someUser = user;
  }).catch(function(error){
    // user not found or any other error
  });