2017-03-06 172 views
0

我試圖從具有一組電子郵件地址的firebase中獲取列表。我只想要與查詢中的電子郵件相匹配的項目。這是結構。我試圖獲取匹配的項目[email protected]collaboratorsAngularefire2 - 查詢未返回預期響應

enter image description here

查詢:

this.firebase.list('/todo', { 
     query: { 
      orderByChild: 'collaborators', 
      equalTo: '[email protected]' 
     } 
    }).subscribe((data)=>{ 
     console.log(data); 
    }); 

這回空數組這應該列出一個項目!

如何解決這個..

解決方案:

let results = this.firebase.list('/todo') 
     .map(data => data.filter((e) => { 
      return e.collaborators.indexOf(email) > -1; 
     } 
    )); 
    return results 

回答

1

你想,如果它是的collaborators一個值來訪問電子郵件地址。實際上,電子郵件地址是其子項之一。

我建議重構你的模型,使用電子郵件作爲關鍵而不是價值;例如,[email protected]: true。我提到了它的一些優點,並提供了一些鏈接,涵蓋了我在下面的評論中的主題。

這個小改動,你的模型後,使用map操作映射到collaborators和使用filter運營商過濾掉的對象,其collaborators對象包含其關鍵比賽email的屬性。

getNotesFromSpecifiedUser(email: string): Observable<any> { 
    let results = this.af.database.list('/todo') 
    .map(data => data.filter(data => (email in data.collaborators))) 
    return results 
} 

重要:如果在collaborators沒有條目的錯誤將被拋出,因爲in關鍵字不能處理空。如果可以想象collaborators將是空的,請告訴我。

+0

我得到這個錯誤'this.firebase.list(...)。map不是函數' –

+1

您需要導入運算符。 'import'rxjs/add/operator/map'' –

+0

這將返回一個空數組 –