簡體   English   中英

Firestore數組包含ngIf

[英]Firestore Array contains ngIf

我在firestore中有以下數據結構:

avisos: { //colection
  documentId1: { //documentId
    titulo: 'myTitle1'
    favoritos: ['three', 'two', 'one']
  },
  documentId2: {
    titulo: 'myTitle2'
    favoritos: ['two', 'five', 'four']
  },
  documentId3: {
    titulo: 'myTitle3'
    favoritos: ['eight', 'six', 'two']
  }
}

所以我想只顯示一個圖標,如果這two值在favoritos矩陣中,那么我嘗試了以下內容:

<div>
  <span>{{ aviso.categoria}}</span> //I have no problems with this
  <mat-icon *ngIf="aviso.favoritos.includes('two')">favorite_border</mat-icon>
</div>

但是我收到以下錯誤:

TypeError:無法讀取未定義的屬性“包含”

我也嘗試了這個,但我得到了同樣的錯誤:

this.favoritos = this.afs.collection('avisos', ref => ref.where('favoritos', 'array-contains', 'two') ).valueChanges();

知道如何解決這個問題嗎?

首先檢查你的組件中是否沒有定義avisos如果是這樣你就不能直接訪問aviso.favoritos你必須將它更改為aviso.documentId1.favoritos同樣的事情,對於documentId2,documentId3也是如此。還使用安全導航oprator ? 為了避免數據加載時出錯,所以你的代碼應該是這樣的

<mat-icon *ngIf="aviso?.documentId1?.favoritos?.includes('two') || aviso?.documentId2?.favoritos?.includes('two') || aviso?.documentId3?.favoritos?.includes('two')">favorite_border</mat-icon>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM