简体   繁体   中英

query from firebase realtime database

I am trying to get filtered data from my realtime database. In this example I want to get only these once with a specific uid:

在此处输入图像描述

I tried to get the data with the snippet from the firebase docs:

const db = getDatabase();
const auth = getAuth();
    
const myUserId = auth.currentUser.uid;
const myOffersRef = query(ref(db, 'allOffers/'), orderByChild('uid'), startAt(user.uid));

But how do I get the data from the const myOffersRef ?

Thank you!

As shown in the documentation on reading data , you can use get to get the data at a path or for a query.

const myOffersRef = query(ref(db, 'allOffers/'), orderByChild('uid'), equalTo(user.uid));

get(myOffersRef).then((snapshot) => {
  snapshot.forEach((child) => {
    console.log(child.key, child.val().uid);
  });
}).catch((error) => {
  console.error(error);
});

Note that I also changed startAt to equalTo , as you likely only want the nodes that match the UID, not the slide of nodes starting from the first match (which is what startAt does).

The forEach is needed since a query can potentially have 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.

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