0
votes

I have been able to resolve the promise that returned me

Promise {<pending>}
__proto__: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: Array(7)

But this time I had the problem when extracting the variable, in this case it turned out, the reason I want to extract it is to store it in a react useState.

This is the code

 const fsdasfda = db
    .collection("socios")
    .where("activity", "==", "true")
    .orderBy("num")
    .get()
    .then(function (query) {
      const array = [];
      query.forEach(function (doc) {
        const data = doc.data();
        array.push(data);
      });
      return array;
    })
    .then((result) => {
      console.log(result);
    });

  console.log(fsdasfda);

const [unidades, setUnidades] = useState(HERE);

console.log (result); it returns the data as I want to pass to the use state. console.log (fsdasfda); the promise returns

As I mentioned earlier I want to pass the RESULT value to the useState

I try this but it still appears as a promise.

fsdasfda.then((data) => console.log(data));

1
The docs property needs to be accessed in order to get the full list of documents from the query. firebase.google.com/docs/reference/js/… - dzv3
fsdasfda.then(data => console.log(data)); this will help you. - Anks
I want to pass to useState [] not setState const [unidades, setUnidades] = useState(HERE) but it is a initial state - Jorge Luis
I believe the only way you can pass the resolved value to useState() is to have its parent component performs the async task and pass the result to it as a prop. - hangindev.com

1 Answers

0
votes

You are getting this log as you are logging promise, add await to your DB query [this code snippet should be inside an async function] and then use fsdasfda.then(data => updateState(data));

const fsdasfda = await db // added await
.collection("socios")
.where("activity", "==", "true")
.orderBy("num")
.get()
.then(function (query) {
  const array = [];
  query.forEach(function (doc) {
    const data = doc.data();
    array.push(data);
  });
  return array;
})
.then((result) => {
  console.log(result);
});
fsdasfda.then(data => {
  console.log(data);
  setUnidades(data); // updating state and logging result data.
});

Check this should work.