Extract data in angular2 with firebase

I tried to get data from firebase using angular2 today without success, I can hide the log data and view the object, but to reuse it I have undefined in the console, maybe the problem with synchronous and asynchronous data is that I want to get this data using angular2?

Thank you

  read(){
    this.publicationUrl  = "https://FBURL/publication";
    this.PublicationRef = new Firebase(this.publicationUrl);
    this.PublicationRef.on("child_added", function(snapshot){
      this.publication = snapshot.val();
      console.log(this.publication)

    })
  }
Run code
+4
source share
1 answer

The problem is that you are setting data to the wrong object this. Use the arrow function to save the lexical scope:

read() {
    this.publicationUrl  = "https://FBURL/publication";
    this.PublicationRef = new Firebase(this.publicationUrl);
    this.PublicationRef.on("child_added", snapshot => {
        this.publication = snapshot.val();
        console.log(this.publication);
    });
}
+3
source

Source: https://habr.com/ru/post/1629268/


All Articles