【问题标题】:How to filter request to Firebase realtime database如何过滤对 Firebase 实时数据库的请求
【发布时间】:2020-03-25 09:52:23
【问题描述】:

我有这样的聊天结构:

我现在想归档聊天,因此用户只能看到他是聊天成员的聊天。 如您所见,每个聊天都有成员节点,有来自 firebase 的 userId。 我如何在我的请求中过滤它? 现在我检索这样的聊天记录:

constructor(props) {
    super(props);
    this.state = { chats: [] };
    this.getChats = this.getChats.bind(this);
  }

  componentDidMount() {
    var _userId = firebase.auth().currentUser.uid;

    this.getChats(_userId);
  }

  getChats = _userId => {
    var readedData = firebase
      .database()
      .ref('chats')
      .orderByKey();
    readedData.once('value', snapshot => {
      this.setState({ chats: snapshot.val() });
      console.log(this.state.chats);
    });
  };

感谢您的帮助! 新结构:

【问题讨论】:

    标签: reactjs firebase firebase-realtime-database


    【解决方案1】:

    以下应该可以解决问题:

    //....
    var uid = 'NGIKTy2......';   //Set the member userId
    var readedData = firebase
      .database()
      .ref('chats')
      .orderByChild('members/' + uid)
      .equalTo(true)
    //....
    

    对应文档:https://firebase.google.com/docs/database/web/lists-of-data#filtering_data

    【讨论】:

    • 你能检查我编辑的问题,图 2 如果我的节点被这样更改,我现在应该如何检查?
    • 您需要遍历第一级子级。
    • 你能编辑你对我节点的回答吗,因为我不太明白怎么做。我只需要检查是否有userId。
    猜你喜欢
    • 2022-06-10
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 2018-06-10
    • 2020-10-25
    • 1970-01-01
    • 2021-01-17
    相关资源
    最近更新 更多