'service function firebase getDocs

I am trying to build a firestore get function to fetch data from firestore database, I successfully fetch the data into the console, but I don't know how to return the data, as everytime that returns a promise.

export const getJobPostings = async () => {
  const jobPostingsRef = collection(database, "jobPostings");
  const snapShot = await getDocs(jobPostingsRef);
  snapShot.forEach((item) => {
    console.log(item.data());
  });
  return snapShot
};


Solution 1:[1]

You don't show how you call this asynchronous function, but since you explained that "everytime (it) returns a promise" you are most probably not calling it correctly.

Since an async function always return a Promise, You should either use then() like for example the following:

getJobPostings()
.then(snapShot => {
  snapShot.forEach((item) => {
    console.log(item.data());
  });
});

or call it with await within an asynchronous function declared with async.

export const anotherAsyncFunction = async () => {
  const snapShot = await getJobPostings();
  snapShot.forEach((item) => {
    console.log(item.data());
  });
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Renaud Tarnec