0

I'm trying to get the current user (if logged in) in an Ember.JS app.

I'm following the general approach outlined by @MilkyWayJoe in this post. This entails having a property on the ApplcatinController like this:

App.ApplicationController = Ember.Controller.extend({
  current_user: null,
  init: function(){
    this.set('current_user', this.store.find("user", "me"));
  }
});

Which works fine, loading the current user when the user is logged in. However when the user is not logged in the server (correctly) returns no user. I get the following error in the console:

Assertion failed: You made a request for a user with id me, but the adapter's response did not have any data

Everything still works, but I'd like to avoid having errors pop up in the console.

So I tried working directly with the promise that find returns:

App.ApplicationController = Ember.Controller.extend({
 current_user: null,
 init: function(){
   var user_promise = this.store.find("user", "me");
   var app_controller = this;
   user_promise.then(function(user){ //Success
     app_controller.set('current_user', user)
   }, function(reason){  //Fail
     //just resolve the promise, no need to do anything 
   })       
 }
});

Which has exactly the same behaviour as the first example: It works, but still prints the same error into the console.

How do I handle the case where the user is not logged in without getting errors in my console?

Community
  • 1
  • 1
Laurie Young
  • 136,234
  • 13
  • 47
  • 54
  • Have you tried using `try..catch`? – Yuck Dec 30 '13 at 12:54
  • How about overriding the Adapter to always return something (even if the server returned nothing) ? http://stackoverflow.com/questions/17938294/how-do-you-create-a-custom-adapter-for-ember-js/17938593#17938593 – edpaez Dec 30 '13 at 14:16

1 Answers1

0

Probably way too late for you, but for future searchers:

DS.Store.find accepts a promise in return. If you're writing your own adapter for the dataservice, instead of calling promise.resolve with the results from your api, you can call promise.reject, which will be handled by Ember without throwing an error in your console.

Garrett Amini
  • 423
  • 3
  • 10