简体   繁体   中英

improperly structuring firebase get call unable to loop over returned data

I have a firebase method that is executing inside of an Object.map:

// firebase call

  const getVisitors = (uid) => {
    let dbRef = ref(db, `organization/${org}/visitors/${uid}`);

    return get(dbRef, (snapshot) => {
      const visitor = snapshot.val();
      return visitor;
    })
  }

// where I execute method above:

    Object.keys(resultData.visitors[item]).map(async visitorEl => {
        const visitorObj = {};
        const individualVisitor = await getVisitors(visitorEl);
        console.log(individualVisitor);
        Object.keys(individualVisitor).forEach(el => {
          console.log(el);
        })
      })

I'm improperly structuring this for an asynchronous call but i'm not sure where I'm going wrong. The first console.log logs the correct data which leads me to believe it's working properly

Object {
  "address": "5722 some street st",
  "bDay": "",
  "city": "Bsome city",
  "email": "",
  "first": "Richard",
  "gender": "M",
  "language": "Spanish",
  "last": "Gongaza",
  "mStatus": false,
  "phone": "323555",
  "profilePic": "",
  "visitDate": "2022-09-11-PM",
  "visitNote": "",
  "zip": "",
}

but the second console.log logs which tells me the data call hasn't resolved.

  _node
    ref
    _index

I await getVisitors. Any idea what I'm doing incorrectly?

I ended up resolving this by breaking up the getVisitors method like so:

  const getVisitors = async (uid) => {
    let dbRef = ref(db, `organization/${org}/visitors/${uid}`);

    const snapshot = await get(dbRef);
    const value = snapshot.val();
    return value;
  }

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