简体   繁体   中英

How to handle error/event when the Ember-Data Store do not find any record?

The situation is:

  • The session.userID has a proper ID
  • However, there is no user data in the backend database (Firebase)

Here is the code:

App.ApplicationRoute = Ember.Route.extend({
  model: function() {
    if (this.session.userID) {
      return this.store.find('user', this.session.userID).then(function(_user) {
        console.log('user found', _user);
        return _user;
      }, function(error) {
        // user does not exist or some other error
        console.log('Application Model did not find user in Firebase');
        return null;
      });
    } else {
      console.log('Application Model did not find any session.userID');
      return null;
    }
  },

}); 

My expectation: The Promisse should call the error function:

function(error) {
  // user does not exist or some other error
  console.log('Application Model did not find user in Firebase');     
});

The error: However, I get the following error:

    Error: no record was found at https://dynamicslife.firebaseio.com/users/facebook%3A931321683548734
        at https://cdn.firebase.com/libs/emberfire/1.2.7/emberfire.min.js:10:3400
        at c (https://cdn.firebase.com/js/client/1.1.2/firebase.js:17:65)
        at https://cdn.firebase.com/js/client/1.1.2/firebase.js:123:689
        at uc (https://cdn.firebase.com/js/client/1.1.2/firebase.js:50:20)
        at Xe (https://cdn.firebase.com/js/client/1.1.2/firebase.js:123:675)
        at Ve.Vb (https://cdn.firebase.com/js/client/1.1.2/firebase.js:123:617)
        at Ye.Vb (https://cdn.firebase.com/js/client/1.1.2/firebase.js:124:418)
        at https://cdn.firebase.com/js/client/1.1.2/firebase.js:139:409
        at https://cdn.firebase.com/js/client/1.1.2/firebase.js:68:878
        at pc (https://cdn.firebase.com/js/client/1.1.2/firebase.js:62:618) ember.js:14876
    Uncaught Error: Assertion Failed: Error: no record was found at https://dynamicslife.firebaseio.com/users/facebook%3A931321683548734 

Question: I know that there is no record to be found. But how to handle this situation and avoid being throw an error? I was under the impression that the Promise would allow me to manage the error before the system crash.

App.ApplicationRoute = Ember.Route.extend({
  model: function() {
    if (this.session.userID) {
      return this.store.find('user', this.session.userID);
    } else {
      console.log('Application Model did not find any session.userID');
      return null;
    }
  },
  afterModel: function(user, transition) {
    if (user.get('length') === 0) {
      console.log('fail');
    } else {
      console.log('success');
    }
  }
});

See http://emberjs.com/api/classes/Ember.Route.html#method_afterModel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM