【问题标题】:Retrieving data based with a where clause in firebase使用firebase中的where子句检索数据
【发布时间】:2020-09-21 19:29:01
【问题描述】:

尝试使用 firebase 数据库存储消息。在使用参考消息检索数据时,我得到了所有消息。

firebase.database().ref('messages').on('child_added', function (snapshot{ 
    console.log(snapshot.val() 
}

如何向它添加过滤器以仅接收该特定用户发送的消息。我需要我提供的用户 ID 发送和接收的消息

【问题讨论】:

    标签: javascript firebase firebase-realtime-database where-clause


    【解决方案1】:

    您需要执行查询:

    firebase.database().ref('messages').orderByChild("user").equalTo("john").on('value', ((snapshot) =>{ 
        console.log(snapshot.val());
    }
    

    假设您有以下数据库:

    messages
       id
        user    : john
        message : message_here
    

    https://firebase.google.com/docs/database/web/lists-of-data

    【讨论】:

    • 但我认为这将返回该用户发送和接收的每条消息。 `````` `````` messages:{ id:{ message: 'abcd' senderid: '123' receiverid: '321 } } `````` 考虑到上述模型,我只想仅在这两个 id 之间的消息
    • 然后查询senderid
    • 这将返回该 id 发送的所有消息。我希望该 id 发送和接收的消息到某个特定的 id
    猜你喜欢
    • 2016-04-05
    • 2019-10-04
    • 2023-03-24
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多