1

I need the value of that.assigned outside the forEach but it would result with undefined if I ever use it as console.log(that.assigned) which is an array

assignedDoctors.then(function(doc){
  let i = 0;
  doc.forEach(function(md){
    tmpMDs.push(md.data());
    tmpMDs[i].key = md.id;
    // tmpMDs.push(md.data().push());
    i++;
  });
  that.assigned = tmpMDs;
}).catch(function(e){
  console.log(e);
});

console.log(that.assigned)
newbie
  • 81
  • 8
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – adiga Feb 10 '19 at 09:39

2 Answers2

0

assignedDoctors.then is a promise function and you can not access value of that variable before calling the promise function.

0

If you are using ES 2017, then you can do it

(async function(){

that.assigned = await assignedDoctors.then(function(doc){
  let i = 0;
  doc.forEach(function(md){
    tmpMDs.push(md.data());
    tmpMDs[i].key = md.id;
    // tmpMDs.push(md.data().push());
    i++;
  });

  return tmpMDs;
});

})()

Otherwise, no way to use the callback.

Arif
  • 6,094
  • 4
  • 49
  • 81