【问题标题】:Error when i try to show a specific Document from Firestore in a ion-card?当我尝试在离子卡中显示 Firestore 中的特定文档时出错?
【发布时间】:2019-09-16 23:25:57
【问题描述】:

当我尝试携带具有特定 ID 的特定文件时,我知道如何携带它。问题是当我尝试在 Html 中显示文档时它不允许我。

调用服务并获取文档的功能!

 item:restaurantModel={
    id_doc: '',
    logo:'',
    descripcion:'',
    nombre:'',
    portada:'',
    idadmin:'',
  }
  
  ResDocument:AngularFirestoreDocument<restaurantModel>;
  ResObservable: Observable<restaurantModel>;
  
  this.idres=this.router.snapshot.params['id'];
           console.log(this.idres);
           this.ResDocument = this.resser.getSpecificRestaurant(this.idres);
           this.ResObservable=this.ResDocument.snapshotChanges().pipe(             
            map(actions => {
              const data = actions.payload.data() as any;
              const uid = actions.payload.id;
              return { uid, ...data};
            })            
          );

//really print the data from a specific document
          this.ResObservable.subscribe(datas => console.log(datas.descripcion));

服务:

getSpecificRestaurant(id){
  return this.fs.doc<restaurantModel>("/restaurantes/"+id);
}

HTML:

<ion-card *ngFor="let item of ResObservable | async ">
    
       
      
    <img src="{{item.portada}}">

   
 <ion-card-content>
   

    <ion-card-title>
        <h1>{{item.nombre}}</h1>
    </ion-card-title> 
  <p>{{item.descripcion}}</p>
  </ion-card-content>
</ion-card>

感谢您的帮助!

【问题讨论】:

    标签: html angular firebase ionic-framework google-cloud-firestore


    【解决方案1】:

    您使用的*ngFor 是可观察的,而不是您在订阅中获得的数据。看到数据后,我认为您不需要使用*ngFor,因为您只会返回一个对象。

    public items;
    
    this.ResObservable.subscribe(datas => this.items = datas);
    

    然后您可以直接在模板中访问该对象。

    <div *ngIf="items">
        <ion-card>      
            <img src="{{ items.portada }}">
            <ion-card-content>
                <ion-card-title>
                    <h1>{{ items.nombre }}</h1>
                </ion-card-title> 
                <p>{{ items.descripcion }}</p>
             </ion-card-content>
        </ion-card>
    </div>
    

    【讨论】:

    • 错误:找不到“object”类型的不同支持对象“[object Object]”。 NgFor 仅支持绑定到 Iterables,例如 Arrays。在 NgForOf.push../node_modules/@angular/common/fesm5/common.js.NgForOf.ngDoCheck 这是错误
    • 你在使用 angular 6 吗?请展示你从 console.log(datas) 得到的结果;
    • 是的,但是没有运行这个选项,错误是:Type 'restaurantModel' is not assignable to type 'restaurantModel[]'.ts(2322) Type 'restaurantModel' is missing the following properties来自“restaurantModel[]”类型:length、pop、push、concat 和 27 more.ts(2740)
    • this.ResObservable.subscribe(datas => {this.items = datas; console.log(this.items); });这告诉你什么,如果我能看到数据就能给你正确的答案
    • 问题是,当我输入 this.items=datas 行时,错误是:“restaurantModel”类型缺少“restaurantModel[]”类型中的以下属性:长度、流行、推送、 concat,还有 27 个。当我显示 console.log(datas) 结果是:{descripcion: "Crepes, Waffles y Helados", idadmin: "KODcVoTqjzNYBWJER7fTkA1cnzp1", logo: "logosenvector.com/logo/img/crepes-waffles-366.jpg", nombre: "Crepes & Waffles", portada: "@ 987654322@"}
    猜你喜欢
    • 2018-02-13
    • 2021-07-12
    • 2021-09-12
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2014-12-22
    相关资源
    最近更新 更多