0

Hello I have a little problem that I dont know how to solve, I have asked a question before because things were not obvious to me but they're now, here, I will write my code and try to explain what I want to achieve later.

This is the data in my database :

posts 
     --- 1 
          --- puslisher -- "Erick" 
          --- content :   "Something is written here"
          --- comments 
                      --- 1
                           --- comment : "Yes"
                           --- commentator : "Patrick"  
                      --- 2 
                           --- comment : "I dont think so "
                           --- commentator : "Sarah"
     --- 2 
          --- puslisher -- "Patrick" 
          --- content :   "News here .."
          --- comments 
                      --- 1
                           --- comment : "Yes"
                           --- commentator : "Ines"  

I get the data in my app.component.ts :

export class AppComponent implements OnInit {

    pubs:Observable<any[]>;
    constructor(private db:AngularFireDatabase){
    }
    ngOnInit(){
       this.pubs = this.getData('/posts');
    }

    getData(path):Observable<any[]>{
       return this.db.list(path).valueChanges()
    }

}

Here is my HTML code :

<div *nfFor="let post of pubs | async ">
    <p>publisher {{post.publisher}}</p>
    <p>content : {{post.content}}</p>
    <div>{{post.comments}}</div>
    <ul> 
       <li *ngFor="let cmt of post.comments" >{{cmt?.comment}}</li>
    </ul>
</div>

But as you can see my first object is empty and that is causing problems to :

publisher: Erick
content : Something is written here 

,[object Object],[object Object]

-                
- Yes
- I don't think so



publisher: Patrick
content : News here 

,[object Object]

- 
- Yes

This is causing me the problem that you can see in the picture.

enter image description here

Any help would be much appreciated.

1 Answer 1

1

Thats because you are using {{post.comments}} it is internally calling Object.toString() method which returns**[object Object]**. So you need to use json pipe to stringify your object and display it.

Try following code.

<div *nfFor="let post of pubs | async ">
    <p>publisher {{post.publisher}}</p>
    <p>content : {{post.content}}</p>
    <div>{{post.comments | json}}</div>
    <ul> 
       <li *ngFor="let cmt of post.comments" >{{cmt?.comment}}</li>
    </ul>
</div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.