我在Firebase上遇到了一些问题。我想做的是,如果聊天室不存在,创建一个新的聊天室,否则,获取已有的聊天室ID。到目前为止,我所实现的是:

数据库结构如下:
chatrooms
chatroomID
participantID1 *(can be senderKey or recipientKey)*
participantID2 *(can be senderKey or recipientKey)*
latestMessage我创建新聊天室的代码如下:
let promiseRoomKey = new Promise((resolve, reject) => {
// need to perform check before insert new chatroom
// create room for chat
var roomKey = firebase.database().ref('chatrooms').push({
[senderKey]: true,
[recipientKey] : true
}).getKey();
resolve(roomKey);
});问题是在我创建新聊天室之前,我需要检查senderKey和recipientKey是否同时存在于同一个聊天室中。如果他们两个都出现在同一个聊天室,我就会抓起chatroomID。如果没有,我将继续创建一个新的聊天室。
但我不知道如何检查senderKey和recipientKey是否同时出现在同一个聊天室。有什么想法吗?
Ps。到目前为止,请忽略latestMessage节点,因为它只是为了方便我的阅读。
谢谢!
发布于 2017-12-26 12:32:20
好吧,IDK如果这是一个好的解决方案,但是你可以创建一个同时具有发送者和接收者in的chatroomID,所以你需要在数据库中搜索是否存在具有这两个in的聊天室。
users
id001:"User Name"
id002:"User Name"
chatrooms
id001id002
id001
id002
latestMessage要在数据库中只搜索一次,您可以对id进行排序,因此它总是首先搜索较低的id,如上面的示例所示。
https://stackoverflow.com/questions/47973806
复制相似问题