AjRev AjRev - 5 months ago 18
AngularJS Question

Unable to access the object properties returned from firebase

This is probably a noob question as I couldn't find any similar questions here in stackoverflow. I am writing an ionic-angularjs application with firebase as the backend. I've a data snapshot returned by firebase snapshot.val() method and this is how the data shows up in my chrome console(attached pic)

var tmp = {};
CurrUser.getEventdesc(eventid).then(function (result) {
tmp = result;
console.log(tmp.Description);
});


CurrUser is a factory and getEventdesc is a function in the factory. The getEventdesc function looks like below:

getEventdesc: function(id) {
var deferred = $q.defer();
ref.child('Events').orderByChild('Eventid').equalTo(id).on('value', function (snapshot) {
event = snapshot.val();
console.log(event);
deferred.resolve(event);
});
return deferred.promise;
}


enter image description here

Now I'm trying to access the 'Description' property by tmp.Description in my angularjs controller but i get an 'undefined' error. I would be grateful if anyone could help me to access the object properties. Thanks for your time.

Update 2:

var tmp = {};
var newtemp = {};
CurrUser.getEventdesc(eventid).then(function (result) {
tmp = result;
for (var itemID in tmp) {
newtmp = tmp[itemID];
};
console.log(newtmp.Description);
});


Regards,

Answer

You're retuning the whole firebase object, not just the requested id, try this:

event = snapshot.val();
deferred.resolve(event[id]);