Home > database >  Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor
Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor

Time:09-30

I'm building a real time chat application with firebase and angular. Actually I'm trying to get the message from the db but i have this error:

Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables, such as Arrays.

service

getMessage(url: string) {
    return this.http.get(url);
  }

logic

messages: any;

ngOnInit(): void {
this.chatService
      .getMessage('https://DB_URL/message.json')
      .subscribe((resMessage) => {
        this.messages = resMessage;
        console.log(this.messages);
      });
}

template

<div *ngFor="let message of messages">
      <p>{{ message }}</p>
    </div>

I don't know to resolve it, surely i'm missing something

I have not posted the whole code because the post would be too long

CodePudding user response:

First, initialize the messages variable as an array:

messages = [];

Then try pushing the message key from the object that you're getting from the server.

this.messages.push(resMessage.yourMessageKey);

  • Related