Firebase Querying Data

query from firebase realtime database

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.

Use query data from Firebase Realtime DB as input for another function

The on method keeps an open listener to the event that can be called repeatedly, so it doesn't return a promise (since a promise can only resolve once). So the await ref.orderByChild....on('child_added'... in your code doesn't do anything, which probably explains the problem.

To properly solve this problem, use once('value', ... and don't combine await and callbacks.

async function queryandgeturls() {
// make the query to the firebase realtimeDB
const results = await ref.orderByChild('timestamp_start').startAt(timestamp1).endAt(timestamp2).once('value');
results.forEach((snapshot) => {
lista.push(snapshot.val().name);
});
...


Related Topics



Leave a reply



Submit