I am an Ember newbie and am trying to create a simple application to fetch records from an API server.
Currently I am trying to query the
this.store.find('validemail',{'email':'abc.gmail.com'}).then(function(response){
console.log(response);
});
//Console log on browser
Class {query: Object, store: Class, manager: Class, isLoaded: true, meta: Object…}__ember1434866946710: "ember592"__ember_meta__: Object__nextSuper: undefinedcontent: (...)get content: GETTER_FUNCTION() {set content: SETTER_FUNCTION(value) {isLoaded: truemanager: Classmeta: Objectquery: Objectstore: Classtype: client@model:validemail:__proto__: Class
This is a lot of data but I am not able to make any sense of it,all I want is a success:true or success:method from the server API.
Here is my Model class import DS from 'ember-data';
export default DS.Model.extend({
success:DS.attr('boolean'),
message: DS.attr('string')
});
and the API method
router.get('/validemails',function(req,res){
console.log(req.query);
var useremail=req.query.email;
Hiveuser.find({'useremail':useremail},function(err,findResponse){
if(!isEmptyObject(findResponse)){
res.send({"validemail": {success: false,'message': 'User Already Exists'}});
}
else{
res.send({"validemail": {success: true,'message':''}});
}
});
});
I realize that I am doing something wrong because Ember Inspector shows 0 records for the model class.
What I don't get is logging the server response shows that the correct response is sent from the server but somehow it gets mangled into this garbled text into the client.
Note: I did not even intend to use Ember Models for this simple use case ,but sending a jQuery GET request would require me to hardcode server IP into the controller ,hence had to go the Adapter "route"(No pun intended :) )
If there is a way I can send a JSON request from the controller which would take the server ip from the Adapter I'd very much like to know about it.
Thanks
First of all, when you execute store.find('modelName')
, Ember REST API Adapter expects an array of models in response. Then, promise of store.find
resolves with array of objects, and you have to get first object to see if success === true
.
this.store.find('validemail', {'email':'abc.gmail.com'}).then(function(validEmails){
console.log(validEmails.get('firstObject.success')); // true || false
});
API method:
router.get('/validemails',function(req,res){
console.log(req.query);
var useremail=req.query.email;
Hiveuser.find({'useremail':useremail},function(err, findResponse){
if(!isEmptyObject(findResponse)){
res.send({"validemails": [{success: false, 'message': 'User Already Exists'}]});
}
else {
res.send({"validemails": [{success: true, 'message':''}]});
}
});
});