找不到类型为“对象”的其他支持对象“ [对象对象]”。 NgFor仅支持绑定到数组等可迭代对象。(AngularFireList)

时间:2018-12-11 22:51:02

标签: angular firebase ionic-framework firebase-realtime-database firebase-authentication

我尝试以this tutorial的形式从Firebase数据库检索数据 但我收到此错误;

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

我想检索当前用户的特定数据(当用户登录到应用程序时)

谁能帮助我解决这个问题

此html页面

<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>

此ts页面

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 个答案:

答案 0 :(得分:0)

this.datanote目前是一个参考,它不包含数据库值,为了获取这些值,您需要等待可观察的值。

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();
}

然后您应该更新HTML,以检查this.datanote上是否存在ngIf="datanote"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>