[英]How to handle error/event when the Ember-Data Store do not find any record?
The situation is: 情况是:
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. 我的印象是Promise可以让我在系统崩溃前管理错误。
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 参见http://emberjs.com/api/classes/Ember.Route.html#method_afterModel
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.