本文介绍了从聊天系统中检索Firebase数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 以下是我的firebase资料 - 用户 - 我有2个问题。 - Q1 - 以有序方式提取user1和user2之间聊天消息的查询,以便在个人聊天窗口中显示 - 我写了以下内容: // refuser1是firebase对https:// myapp .firebaseio.com / messages / user1 var messages = []; refuser1.orderByChild(from)。equalTo(user1)。once('value',function(s){ s.forEach(function(childsnap){ messages。 push(s.val()); }); }); $ b refuser1.orderByChild(from)。equalTo(user2)。once('value',function(s){ s.forEach(function(childsnap ){ messages.push(s.val()); }); }); //现在消息数组中包含user1和user2之间的所有消息 - //添加代码以基于时间戳的数组对消息进行排序/ b $ b 这是存储和检索个人一对一聊天数据的正确方法吗? Q2 - 检索数据时,'价值'和'child_added'事件似乎行事不同 - 见下图 - 当我使用'值'我得到所有的子对象,但当我使用'child_added'我只得到users1中的第一个子项。 文件说明每个初始孩子都会触发child_added(假设这意味着在添加新孩子之前存在的所有孩子)我的理解是否正确?我期望在'value'和'child_added'中返回相同的结果 必须以您想要的方式存储数据。如果您想按特定顺序获取特定用户之间的聊天消息,则应该以这种方式存储它们。 聊天记录 user1_user2 -K ... c9 from:user1 message:Hello message 1 time: -K .. .od from:user1 message:Hello message 2 time: -K ... t8 from:user2 message:Hello message 1 time: -K ... c9 from:user1 message:Hello message 1 time: -K ... xb from:user2 message:Hello message 2 time: $ b 在上面的结构中,我将 user1 和 user2 在一个节点 user1_user2 下。这个节点作为这两个用户之间的私人聊天室:任何时候同一个两个用户聊天,他们的消息都被添加到这个房间。 Q2)重复执行一次('child_added'。这意味着您要告诉Firebase仅触发一次,然后再触发 child_added 停止射击它,如果你反而做 on('child_added'它会触发每个initila孩子和所有后续添加。 Here is my firebase data - I store a copy of every message under both the users -I have 2 questions. -Q1 - The query for extracting chat messages between user1 and user2 in an ordered fashion so it can be displayed in the personal chat window - I wrote the following- //refuser1 is firebase reference to "https://myapp.firebaseio.com/messages/user1"var messages = []; refuser1.orderByChild("from").equalTo("user1").once('value', function(s){s.forEach(function(childsnap){messages.push(s.val());});});refuser1.orderByChild("from").equalTo("user2").once('value', function(s){ s.forEach(function(childsnap){ messages.push(s.val()); });});// Now messages array has all messages between user1 and user2 -// add code to sort messages in array based on timestamp /Is this the correct way to store and retrieve data for personal one to one chat?Q2 - When retrieving data , 'value' and 'child_added' events seem to act differently - See the image below - When i use 'value' i get all the child objects but when I use 'child_added' I get only the first child in users1 .The documentaion says that child_added is triggered for every initial child (assuming this means all the child that were existing before new child is added)Is my understanding correct? I was expecting the same returned result for 'value' and 'child_added' 解决方案 Q1) In NoSQL databases, you'll typically have to store the data in the way you want to use it. If you want to get the chat messages between specific users in a specific order, you should store them in that way.chats user1_user2 -K...c9 from: "user1" message: "Hello message 1" time: "" -K...od from: "user1" message: "Hello message 2" time: "" -K...t8 from: "user2" message: "Hello message 1" time: "" -K...c9 from: "user1" message: "Hello message 1" time: "" -K...xb from: "user2" message: "Hello message 2" time: ""In the above structure I've grouped the messages between user1 and user2 under a node user1_user2. This node serves as the "private chat room" between these two users: any time the same two users chat, their messages are added to this room.Q2) You're doing once('child_added'. This means that you're telling Firebase to fire child_added only once and then to stop firing it. If you instead do on('child_added' it will be triggered for every initila child and all subsequent additions. 这篇关于从聊天系统中检索Firebase数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-16 19:33