0

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": "[email protected]",
        "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.

5
  • You can get the value using: data.-Lhdfgkjd6fn3AA-.email Commented Jul 2, 2018 at 1:17
  • @NullPointer No, you can't. Use bracket notation instead. Commented Jul 2, 2018 at 1:19
  • @CertainPerformance-Ya.I missed thanks Commented Jul 2, 2018 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" Commented Jul 2, 2018 at 1:21
  • Can you please tell me how to access -Lhdfgkjd6fn3AA- from the above data? Commented Jul 2, 2018 at 1:50

3 Answers 3

2

let data = {
 "-Lhdfgkjd6fn3AA-": {
        "email": "[email protected]",
        "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

Sign up to request clarification or add additional context in comments.

10 Comments

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: That is not the data that you've shared in the question
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": "[email protected]", "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: 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)
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.
|
1

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);
}

Comments

0

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.

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.

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);
    });
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.