我有一个Firebase数据库构造问题.我的场景接近一个聊天应用程序.以下是具体细节
- users(node storing several users of the app) - id1 name: John - id2 name: Meg - id2 name: Kelly - messages(node storing messages between two users) - message1 from: id1 to: id2 text: '' - message2 from: id3 to: id1 text: ''
现在想象一下,为个人用户建立一个会话视图.所以我想要从该特定用户获取所有邮件
并对该特定用户
我现在写如下:
let fromMessagesRef = firebase.database().ref('messages').orderByChild('from').equalTo(firebase.auth().currentUser.uid) fromMessagesRef.once("value").then((snapshot) => {/* do something here*/}) let toMessagesRef = firebase.database().ref('messages').orderByChild('to').equalTo(firebase.auth().currentUser.uid) toMessagesRef.once("value").then((snapshot) => {/* do something here*/})
问题:
解决方法
我会存储这样的数据:
- users(node storing several users of the app) - id1 name: John messages message1: true message2: true - id2 name: Meg messages message1: true message3: true - id3 name: Kelly messages message2: true message3:true - messages(node storing messages between two users) - message1 from: id1 to: id2 text: '' - message2 from: id3 to: id1 text: '' - message3 from: id2 to: id3 text: ''
Firebase推荐存储这样的东西.所以在你的情况下你的查询是
let fromMessagesRef = firebase.database().child('users').child(firebase.auth().currentUser.uid).child('messages')