0

I am trying to fetch the firestore data and then store it in a variable

async function getchildContent(Parent, Message) {
    let count = 0;
    var db = firebase.firestore();
    var output = db.collection("rooms").doc(Parent).collection("messages").where("ParentId", "==", Message).get().then(function (querySnapshot) {
        querySnapshot.forEach(function (doc) {
        })

    });

    return output;
}

This function i am calling as

var dataAll ={};
getchildContent(PDocId, doc.id).then(result=>{
    dataAll = result;
});

console.log(dataAll);

If i assign it as

var output = getchildContent(PDocId, doc.id);

Then the result will br promise {pending}

But I am getting an empty array output, How to resolve this?

1
  • Try return db.collection("rooms").d...... Commented Jul 20, 2021 at 6:41

1 Answer 1

1

You should handle the promises using either a promise chain or async-await. It seems you want to use async await syntax so try this:

async function getchildContent(Parent, Message) {
  let count = 0 ; 
  const db = firebase.firestore();
  const querySnapshot = await db.collection("rooms").doc(Parent).collection("messages").where("ParentId","==",Message).get()
  const data = querySnapshot.map.docs((doc) => doc.data())
  return data
});
             
// This should be inside of an async function as you need to use await.
const output = await getchildContent(PDocId, doc.id);
// If you are using promise chainging
let output = null

getchildContent(PDocId, doc.id).then((data) => {
  console.log(data)
  output = data

  // logging output here will log the value
  // proceed code execution within then block
})

// If you log output here, it'll be null as then promise above will not be resolved

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.