Firebase数据库如何列出

时间:2017-02-06 12:46:07

标签: javascript firebase firebase-realtime-database

我正在尝试查询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()
    })
  }

1 个答案:

答案 0 :(得分:0)

以下过滤器有效:

self.emb