Retrieving Request Properties

When prototyping login functions for my ember app, I use a route to make a store request based on a given username. If this username is not found, my API returns an object with a message property. This route is:

App.LoginRoute = Ember.Route.extend({
    actions: {
        getUsername: function(username){
            this.store.find('user', {username: username}).then(function(user){
            var eid = user.get('eid');
            console.log(eid);
            if (eid) {
                self.controller.send('transition', "index");
                }
            else {
                self.controller.set('model', "Oops! That not right.");}
                });
            }
    }
});`

      

If the username exists in the database, the API will send back the custom object. It loads into the store just fine, if the username exists, I can see the entry in the Data in Ember Inspector. However, I cannot figure out how to get the properties of this custom object.

In .then

I pass the return information and try to call .get

on this, but it always returns undefined.

What is the correct way to get the properties of what is returned from store.find('store', {query})

?

+3


source to share


1 answer


find returns a collection upon request.

this.store.find('user', {username: username}).then(function(userCollection){
  // this would be the user if it existed
  var user = userCollection.get('firstObject');
  self.controller.send('transition', "index");
});     

      



You should probably return a 404 error code instead of a valid response when the user doesn't exist, and then remove the failed promise part.

this.store.find('user', {username: username}).then(function(userCollection){
  // this would be the user if it existed
  var user = userCollection.get('firstObject');
  self.controller.send('transition', "index");
}, function(){
  // failure happened
  self.controller.set('model', "Oops! That not right.");}
});     

      

+2


source







All Articles