Reputation: 175
In prototyping a login functionality to my ember app, I am using the route to make a query to the store based on a given username. If that username is not found, my API returns an object with a message property. Here is that route:
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's not right.");}
});
}
}
});`
If the username exists in the databse, the API will send back user object. It loads into the store just fine if the username exists, I can see the record under Data in Ember Inspector. However, I cannot figure out how to get the properties of that user object.
In the .then
I am passing the returned information, and attempting to call .get
on that, but that always returns undefined.
What is the proper way to get the properties of whatever is returned from a store.find('store', {query})
?
Upvotes: 3
Views: 34
Reputation: 47367
find by query returns a collection.
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 probably should be returning a 404 error code instead of a valid response when the user doesn't exist, then hit the failure portion of the promise.
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's not right.");}
});
Upvotes: 2