2017-02-06 49 views
0

我想查詢Firebase數據庫。我有下面的代碼,但是當有匹配的數據時它會返回一個空列表。Firebase數據庫如何列出

loadData() { 
    this.firelist = this.af.database.list('/chat/', { 
     query: { 
     orderByChild: 'negativtimestamp' 
     } 
    }).map(items => { 
     const filtered = items.filter(item => { 
     item.memberId1 === this.me.uid; 
     }); 
     return filtered; 
    }); 

    // if me not in firelist then create new chat 
    if (this.me && this.me.uid) { 
     let chatFound: boolean = false; 
     console.log('this.firelist', this.firelist); 
     this.firelist.forEach(chatItems => { 
     console.log('chatItems', chatItems); 
     for (let i = 0; i < chatItems.length; i++) { 
     console.log('chatItems['+i+']', chatItems[i]); 
      let memberId1: string = chatItems[i].memberId1; 
      let memberId2: string = chatItems[i].memberId2; 
      if (memberId1 === this.me.uid || memberId2 === this.me.uid) { 
      chatFound = true; 
      break; 
      } 
     } 
     if (!chatFound) { 
      //this.createChat(); 
     } 
     }); 
    } 
    } 

我覺得我的問題是與filter

下面的代碼創建一個chat成功:

createChat(img1: string, img2: string) { 
    this.af.database.list('/chat/').push({ 
     memberId1: this.me.uid, 
     memberId2: this.you.uid, 
     img1: img1, 
     img2: img2, 
     displayName1: this.me.displayName, 
     displayName2: this.you.displayName, 
     lastMsg_text: 'todo', 
     timestamp: Date.now(), 
     negativtimestamp: -Date.now() 
    }) 
    } 

回答

0

以下過濾器的工作原理:

this.firelist = this.af.database.list('/chat/', { 
    query: { 
    orderByChild: 'negativtimestamp' 
    } 
}).map(items => { 
    const filtered = items.filter(
    item => item.memberId1 === this.me.uid 
    ); 
    return filtered; 
});