Skip to content Skip to sidebar Skip to footer

How Do I Filter The Data In Documents In Firebase In Js And Get Promiseresult In A Promise

How do I get the PromiseResult in JS Here's the code I tried: total_amount = db.collection('users').get().then((querySnapshot) => { const sum = querySnapshot.docs.reduce(

Solution 1:

So, a few things. You appear to be calling a function that returns a promise, and before awaiting it to finalize, you log it.

You could wrap a Promise.All() onto it, but this requires the parent function to be async which might not be ideal.

So instead we declare the function as a const and define it as an async function You can modify this to suite your code, but without knowing the full parent block, I can only make some assumptions.

const getUsers = async()=>{
total_amount = await db.collection("users").get().then((querySnapshot) => {
    const sum = querySnapshot.docs.reduce((a, b) => a + b.data().amount, 0)
    return sum
  });
  console.log(total_amount);
}

getusers();

Post a Comment for "How Do I Filter The Data In Documents In Firebase In Js And Get Promiseresult In A Promise"