Home > database >  I have problem with read promise from async function in my react app with
I have problem with read promise from async function in my react app with

Time:01-21

When I console.log($a) in readData function a get object, but when I use this function in another component I get promise. I need to get object not promise.

async function readData(collectionName) {
  try {
    const querySnapshot = await getDocs(collection(db, collectionName));
    let $a = [];
    querySnapshot.forEach((doc) => {
      $a.push({ id: doc.id, ...doc.data() });
    });
    console.log($a); // get object
    return $a;
  } catch (err) {
    console.error(err);
  }
}
const addProd = () => {
    const products = readData("products");
    console.log(products); // get promise, but I need object
  };

CodePudding user response:

Add await to the

const products = await readData("products");

This is because you are not waiting for the promise to be resolved and simply console logging straight away

  •  Tags:  
  • Related