4

i tried to retrieve data from firebase with angular2 today without success, i can console log data and see object, but for reusing them i have undefined in the console, maybe an issue with synchronous and asynchronous data, what kind of solution i have to retrieve these data with 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)

    })
  }

1 Answer 1

3

The issue is that you set data to the wrong this object. Use arrow function to preserve 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);
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

hi thanks for your answer, i can console.log datas , and see objects but cannot use them because they are undifined , when i ngfor the datas with json pipe i can see them... i build and custom pipe to transform value and args to return what i want , i have this error Cannot convert undefined or null to object

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.