2
votes

I tried to retrieve data from firebase database as this tutorial but I get this error;

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

I want to retrieve specific data for the current user (when user login to the app)

can anyone help me to solve this problem

this html page

<ion-content padding>
  <!--  مسسوول عن عرض البيانات  -->
  <ul *ngFor ="let d of datanote " class="collection" class="colorul" text-right >
    <li class= "collection-item" >
      <ion-card class="backgounddiv" >     
        <ion-card-header>
          <strong class="colorli" >{{d.title}}<br></strong>
        </ion-card-header>
        <ion-card-content>
          <strong class="colorli" >{{d.desciption}}</strong>
        </ion-card-content>   
      </ion-card>
    </li>
  </ul>
</ion-content>

this ts page

export class HomePage {
  datanote :AngularFireList<users[]> ;
  userId: string;

  constructor(
    public navCtrl: NavController,
    private ev: Events,
    public firestoreService:FierbaceserverProvider,
    public navParams: NavParams, 
    private afAuth: AngularFireAuth,
    private db: AngularFireDatabase 
  ) {  }

  ngOnInit():AngularFireList <users[]> {
    if (this.userId) return;

    this.datanote=this.db.list(`note-list/${this.userId}`);

    return this.datanote; 
  }
}
1
If you do, console.log(this.datanote) what do you see? Object? Array? Also ngOnInit() returns void see angular doc - penleychan
If you do, console.log(this.datanote) ?nothing - Hanan

1 Answers

0
votes

this.datanote is a reference at the moment, it doesn't contain the database values, in order to get these values you need to wait for the observable.

private noteList$: Subscription;

ngOnInit() {
  if (this.userId) return;

  this.noteList$ = this.db.list(`note-list/${this.userId}`)
    .snapshotChanges()
    .subscribe(snapshot => {
      this.datanote = snapshot;
    });
}

ngOnDestroy() {
  noteList$.unsubscribe();
}

You should then update your HTML to check if this.datanote exists with an ngIf="datanote" on the ion-content.

<ion-content *ngIf="datanote" padding>
  <!--  مسسوول عن عرض البيانات  -->
  <ul *ngFor ="let d of datanote " class="collection" class="colorul" text-right >
    <li class= "collection-item" >
      <ion-card class="backgounddiv" >     
        <ion-card-header>
          <strong class="colorli" >{{d.title}}<br></strong>
        </ion-card-header>
        <ion-card-content>
          <strong class="colorli" >{{d.desciption}}</strong>
        </ion-card-content>   
      </ion-card>
    </li>
  </ul>
</ion-content>