简体   繁体   中英

Cannot read JSON value from Firebase

I'm on a react-native project where I'm querying firebase to get some data from Firebase realtime database. However I receive my required output by below code.

getData = (email) => {
let usersRef = firebase.database().ref('users');
usersRef.orderByChild('emailAddress').equalTo(email).on("value", (snapshot) => {
  let data  = snapshot.val();
  console.log(data);
  console.log(data.emailAddress);
});

}

But unexpectedly I get the output for second log as undefined .

Chrome Inspect中的Firebase返回视图

Problem

I cannot read the value of emailAddress . I don't understand why. Below is my database structure.

数据库结构

I was missing foreEach function since it returns proto as well.

getData = (email) => {
  let usersRef = firebase.database().ref('users');
  usersRef.orderByChild('emailAddress').equalTo(email).on("value", (snapshot) => {
    snapshot.forEach((snap) => {
      console.log(snap.val().emailAddress);
    })
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM