CSDUG
CSDUG

Reputation: 239

Getting data from FIrebase realtime database

For example, this is how I set data:

var usersRef = firebase.database().ref("userlist/");
usersRef.child(nickname).set({
    userid: id,
    email: email,
    });

What should I do if I later on want to get email given the nickname? Why can't I do something like this:

const email =usersRef.child(nickname).child(email);

Upvotes: 1

Views: 429

Answers (2)

Lucem
Lucem

Reputation: 3082

The email constant that you set does not point to an email rather it points to a firebase database reference.

To get the value of email, you listen to the reference and the value is equal to what is returned.

ref.child(nickname).child("email").on("value", function(snapshot) { 
    const email = snapshot.val();
  });

Upvotes: 1

Peter Haddad
Peter Haddad

Reputation: 80914

To get the email you need to do the following:

 var ref = firebase.database().ref("userlist").child(nickname);

ref.on("value", function(snapshot) { 
   let emails=snapshot.val().email;
    console.log(email);
  });
});

You need to reference the location and then attach a listener to it to be able to retrieve the child values like email and id

Upvotes: 2

Related Questions