Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Error while loading route: undefined " when setting route model hook #39

Closed
jrhe opened this issue Apr 10, 2014 · 11 comments
Closed

"Error while loading route: undefined " when setting route model hook #39

jrhe opened this issue Apr 10, 2014 · 11 comments

Comments

@jrhe
Copy link

jrhe commented Apr 10, 2014

I followed the readme to get a basic setup, added a model and then implemented a model hook in a route to return all of the model. This throws the error:

Error while loading route: undefined

I have also tried creating records but they don't seem to get persisted but this may be a separate issue.

I have compiled a minimal jsbin of the problem:

http://emberjs.jsbin.com/kuyujohi/14/edit

App = Ember.Application.create();

App.ApplicationAdapter = DS.FirebaseAdapter.extend({
  firebase: new Firebase('https://emberfire-test.firebaseio.com')
});

App.Post = DS.Model.extend();

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('post');
  }
});
@stefanpenner
Copy link
Collaborator

I am unsure if it is directly related, but I noticed.

https://github.com/firebase/emberFire/blob/master/src/data.js#L181 doesn't enqueue an actual error object, which likely is resulting in a poor error message.

This is due to crappy error logging in the router: https://github.com/emberjs/ember.js/blob/master/packages_es6/ember-routing/lib/system/router.js#L525
opened an issue: emberjs/ember.js#4691

But in general it is best practice to reject with error objects

@aputinski
Copy link
Collaborator

@jrhe I went to https://emberfire-test.firebaseio.com/posts.json in the browser and it looks like your Firebase doesn't have any data at the /posts location

@stefanpenner I'll update that so it throws an actual error.

@bnjamin
Copy link

bnjamin commented Apr 11, 2014

@aputinski Does that mean you have to bootstrap your firebase with some data? If not, how are you then supposed to handle empty collections?

@mbykovskyy
Copy link

Luckily I haven't hit this problem yet because I'm accessing a non-existing child collection through a record that does exist in firebase and it works well. But I see a future use case where I will hit this problem. I went through the code to understand why it was working in my case and interestingly I noticed that if you access a non-existing collection (could be anything really) through a parent record like so this.modelFor('post').get('comments') emberFire gets the post and then emberData converts comments attribute to an Ember.Array which in turn converts an undefined to an empty array.

Usually it's a good practice to return empty objects as opposed to null or undefined, however, how would you detect that resource doesn't exist in that case? If one chooses to display an error message for example.

You can handle the rejection like so,

var route = this;

return new Ember.RSVP.Promise(function(resolve, error) {
  resolve(route.store.find('post'));
}).then(function(value) {
  return value;
}, function(reason) {
  return [];
});

@stefanpenner
Copy link
Collaborator

simplification of @mbykovskyy example

route.store.find('post').catch(function(reason) {
  // check if reason tells us it was just "not found"
  return [];
});

@mbykovskyy
Copy link

Nice one @stefanpenner!

@aputinski
Copy link
Collaborator

@stefanpenner would there be any harm in returning an empty array in the adapter if no data was found instead of rejecting?

https://github.com/firebase/emberFire/blob/master/src/data.js#L257

@stefanpenner
Copy link
Collaborator

@aputinski I am unsure

@jrhe
Copy link
Author

jrhe commented Apr 11, 2014

I knew that I had no data but I couldn't figure out how to create it
correctly through the GUI and I assumed that that it would have created the
collection if I tried to create a record.

On 11 April 2014 17:54, Stefan Penner notifications@github.com wrote:

@aputinski https://github.com/aputinski I am unsure

Reply to this email directly or view it on GitHubhttps://github.com//issues/39#issuecomment-40225338
.

aputinski added a commit that referenced this issue Apr 12, 2014
Unfound records are now dematerialized.
jrhe added a commit to jrhe/emberFire that referenced this issue Apr 12, 2014
@jrhe
Copy link
Author

jrhe commented Apr 13, 2014

@aputinski Your fixes work well.

@jrhe jrhe closed this as completed Apr 13, 2014
@idesignedme
Copy link

will using {{link-to 'HOME' 'home'}} break an app? also is this discouraged?
should I be using {{#link-to}}HOME{{/link-to}} instead. I'm wondering if this is the reason my app seems to break

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants