首页 > 解决方案 > 使用firebase中的where子句检索数据

问题描述

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

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

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

标签: javascriptfirebasefirebase-realtime-databasewhere-clause

解决方案


您需要执行一个查询:

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


推荐阅读