How can I pass data into a const

#1

Hi, in my code below, I have data in the const myData I am checking it by console logging it out.

I would like to then pass that into my { data } when I create the modal (third line from bottom)

{ data: this.myData }

this is now console logging as undefined when I try to console log it out as this.myData

openModal(post) {
    
    var docRef = firebase.firestore().collection("posts").doc(post.id)
    docRef.get().then(function(doc) {
      if (doc.exists) {
          console.log("Document data:", doc.data())
          const myData = doc.data()
          console.log("My data:", myData)
          
      } else {
          console.log("No such document!");
      }
    }).catch(function(error) {
        console.log("Error getting document:", error);
    });
    const mapModal = this.modal.create('MapPage', { data: this.myData });
          mapModal.present();
  }
#2

Never type the word function inside the body of one. Use fat arrow functions instead. Put the mapModal stuff inside the then block, in which doc is valid. Try to avoid putting doc into a controller property (myData). const in JavaScript is IMHO worthless, so I would simply use let everywhere you have const.

#3

Thank you Rapropos, I finally managed to do this. I am stuck on the next error now:

Error: Cannot find a differ supporting object ‘[object Object]’ of type ‘object’. NgFor only supports binding to Iterables such as Arrays.

#4

any help here please?

#5

first i think it has to do with the scope of myData

you dont declare it until inside the IF-THEN statement, and then go on to use it outside. I would start with

let docRef = firebase.firestore().collection("posts").doc(post.id);
let myData: any;
if (doc.exists) {
    console.log("Document data:", doc.data());
    myData = doc.data();
    console.log("My data:", myData);

or I would try moving the modal creating to within the if block. I assume you do not want the modal to show if no data comes through?
this would also take care of the scope issue


The error here looks like you may be getting data that is not iterable, firebase usually gives us observables. try just logging to see what the data coming back looks like

#6

well, if I wrap it in JSON.Stringify() it outputs as what I believe would be iterable data but then I run into other errors about can’t iterate through a string and something about the *ngFor loop

#7

what is the output from

?