Reputation: 44
I want to get the value(111122223333) of the push id in AllocEmp. firebase DB:
{
"AllocEmp" : {
"-L3c47chqb35p0Ndo_XB" : "111122223333"
},
"complaintDate" : "Wed Jan 10 18:40:34 GMT+05:30 2018",
"complaintDesc" : "ggshwhwuuww\ngshhsjsn",
"complaintLM" : "annA",
"complaintLoc" : "ist",
"complaintName" : "Continous Fluctuations",
"defaultTime" : "Thu Jan 11 00:40:34 GMT+05:30 2018",
"department" : "Electricity",
"resolvingTime" : "6",
"uid" : "123456788765"
}
im using the code:
var fb =
firebase.database().ref().child('UserComplaints').child('AllocEmp');
fb.once('value', snap => {
alert(snap.val);
});
this code returns null. please help me
Upvotes: 1
Views: 1234
Reputation: 457
I faced the similar issue. What happens here is that call backs are run later so console only tells that it is an object. So u can either use wait () and set Timeout . But obviously it is a wrong practice. Enable a function call in the code to make it synch . Hope it helps.
Upvotes: 1
Reputation: 12675
In your code you are using snap.val
. Since val is a function not an expression. Use snap.val()
. Also remeber it returns an object so try to console it not alert or you can stringify the object to alert.Like this :-
let values = []
var fb =
firebase.database().ref().child('UserComplaints').child('AllocEmp');
fb.once('value', snap => {
console.log(snap.val()); // to console object
Object.keys(snap.val()).map(k => {
console.log(snap.val()[k])
values.push(snap.val()[k])
})
alert(JSON.stringify(snap.val())) // to alert object
});
console.log(values)
Upvotes: 0
Reputation: 18592
you're not calling val()
method , you're missing the parentheses
what you should do is
var fb = firebase.database().ref().child('UserComplaints/AllocEmp/1515589858255');
fb.once('value', snap => {
alert(snap.val()); //here is the problem
});
Upvotes: 1