React, Firebase: Access values of JSON and also get key value

Multi tool use
Multi tool use


React, Firebase: Access values of JSON and also get key value



I am beginner working with firebase, react. I am able to get the required data from firebase based on userEmail. But I am very confused in accessing the data.


firebase.database().ref('/users').orderByChild('email').equalTo(userEmail).on('value', data => {
console.log('data: ', data);
})



I get the following output:


data: Object {
"-Lhdfgkjd6fn3AA-": Object {
"email": "t5@gmail.com",
"favQuote": "this is it",
"firstName": "t5",
"lastName": "l5",
},
}



Please help me how to access all values ("-Lhdfgkjd6fn3AA-" , firstname, lastname, email and favQuote) into variables like: data.firstName, data.lastName, data.key, etc . Thank you.





You can get the value using: data.-Lhdfgkjd6fn3AA-.email
– NullPointer
Jul 2 at 1:17






@NullPointer No, you can't. Use bracket notation instead.
– CertainPerformance
Jul 2 at 1:19





@CertainPerformance-Ya.I missed thanks
– NullPointer
Jul 2 at 1:21





-Lhdfgkjd6fn3AA- should be id which is unpredictable, and hence in his program I don't think he can pre guess the "key"
– Isaac
Jul 2 at 1:21


-Lhdfgkjd6fn3AA-





Can you please tell me how to access -Lhdfgkjd6fn3AA- from the above data?
– kumar111
Jul 2 at 1:50




3 Answers
3




let data = {
"-Lhdfgkjd6fn3AA-": {
"email": "t5@gmail.com",
"favQuote": "this is it",
"firstName": "t5",
"lastName": "l5",
},
};

console.log(Object.keys(data))//returning an array of keys, in this case ["-Lhdfgkjd6fn3AA-"]
console.log(Object.keys(data)[0])
console.log(Object.values(data))//returning an array of values of property
console.log(Object.values(data)[0].email)



Do need to be careful that the above code with the hardcoded "0" as index because it assumed that your data object has only one key. If you have more key, you can't simply replace index either because property of object has no predictable sequence


data





I was not able to retrieve the values. This is what I got: data: Array [ 21:23:58: "node_", 21:23:58: "ref_", 21:23:58: "index_", 21:23:58: ] 21:23:58: dat1: node_ 21:23:58: dat2: Array [ 21:23:58: ChildrenNode { 21:23:58: "children_": SortedMap { 21:23:58: "color": false, ............. 21:23:58: "key": "-Lhdfgkjd6fn3AA-", 21:23:58: "left": LLRBEmptyNode {}, 21:23:58: "right": LLRBEmptyNode {},
– kumar111
Jul 2 at 1:29






@kumar111: That is not the data that you've shared in the question
– Isaac
Jul 2 at 1:31





When I print data after async from firebase will give me the one that I posted in my question. But when I tried to print using Object.keys(data), then it prints entirely a different output. I am not sure why. data.key gives me users data gives me: data: Object { "-Lhdfgkjd6fn3AA-": Object { "email": "t5@gmail.com", "favQuote": "this is it", "firstName": "t5", "lastName": "l5", }, } but Object.keys(data) gives me : object data: Array [ 21:41:32: "node_", 21:41:32: "ref_", 21:41:32: "index_", 21:41:32: ]
– kumar111
Jul 2 at 1:42






@kumar111: You've just said it yourself, data.key gave you the users data, so instead of just Object.keys(data), change it to Object.keys(data.key)
– Isaac
Jul 2 at 1:44


data.key


Object.keys(data)


Object.keys(data.key)





Figured it. Using data.toJSON() worked. let data2 = data.toJSON() console.log(Object.keys(data2)[0]) - gave me the key console.log(Object.values(data2)[0].email) - gave me email thank you.
– kumar111
Jul 2 at 2:48




It's really a JavaScript question. I had to figure this out too. ...this works.


var p;
var thisLine;
p = docu.data();
for (var k in p) {
if (p.hasOwnProperty(k)) {
if (isObject(p[k])) {
thisLine = p[k];
Object.keys(thisLine).forEach(function (key, index) {
console.log(key, index);
});
}
}
}

function isObject(obj) {
return obj === Object(obj);
}



When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.



So your first step is that you need to loop over the snapshot in your on() callback.


on()



The second step is that you need to call Snapshot.val() to get the JSON data from the snapshot. From there you can get the individual properties.


Snapshot.val()


firebase.database().ref('/users').orderByChild('email').equalTo(userEmail).on('value', snapshot => {
snapshot.forEach(userSnapshot => {
let data = userSnapshot.val();
console.log('data: ', data);
console.log(data.email, data.firstname);
});
})






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

PnW8 X16NC0FuHIJ0Nv1e868gI,5f8oOn,Bkf00,Y,PnvuTA2fAAX7hhU W6gtkcc7KaP,Vrwp0g3GZYb
ooo GCDLk6kFiFv4uk54b2WMdEr4iwxre4cctr9UO31rA8xnsoNnqX7G6HP84,VI2QtwxDwYMvHCua 0

Popular posts from this blog

Rothschild family

Cinema of Italy