0

I am writing a program that is accessing a database and returning a specific value stored in the database. In this particular instance, I am accessing a database which has a document called "basementER" and then accessing its "status" field. This field is supposed to contain the value of "0".

The program accesses this value and even prints it to the console correctly inside of the method. However, when I try to return this value and print it to the console from the outside, I get "undefined"?

Why could this be happening? It doesn't make sense to me. Thanks in advance!

Here is my code and with comments I show the results

 var testRoom = "basementER";

  var rawStatus = getRawStatus(testRoom);
  console.log(rawStatus);  //prints undefined to console

  function getRawStatus (room)
  {
    db.collection("rooms").doc(room).get().then(function(doc) {
      console.log(doc.data().status); //prints 0 to console
      var rawStatus = doc.data().status;
      console.log(rawStatus); //prints 0 to console
      return rawStatus;
    })
  }//end getRawStatus
  • 1
    Please put a [mre], **as text**, in the question. – jonrsharpe Mar 29 '20 at 20:00
  • 2
    Don't post images of code or error messages. Post the code (or the error message) as text. Use the `{}` button in the question editor to format it nicely as code. – axiac Mar 29 '20 at 20:01

0 Answers0