如何在Firebase查询中使用startAt()? [英] How to use startAt() in Firebase query?

查看:87
本文介绍了如何在Firebase查询中使用startAt()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设上面的Firebase数据库架构.

Let's suppose above firebase database schema.

我要检索的消息是在"15039996197"时间戳之后的消息.每个消息对象都有一个createdAt属性.

What I want to is retrieve messages which after "15039996197" timestamp. Each of message object has a createdAt property.

如何仅在特定时间戳记后才能收到消息?在这种情况下,最后两条消息就是我要检索的消息.

How can I get messages only after specific timestamp? In this case, last two messages are what I want to retrieve.

我尝试了firebaseb.database().ref(`/rooms/$roomKey/messages`).startAt('15039996197'),但是失败了.返回0.我该怎么办?

I tried firebaseb.database().ref(`/rooms/$roomKey/messages`).startAt('15039996197') but failed. It return 0. How can I do this?

推荐答案

查询的情况是,期望message节点应以数字开头,在这种情况下,您需要一个子节点名称createdAt.因此,在这种情况下,您必须指定要按createdAt进行订购,因此您需要这样做

The case with your query is that it's expecting that the message node should have a number value to start with, in that case you want a child node with the name createdAt. So in that case you must specify that you want to order by createdAt, thus you need to do this

firebase.database().ref(`/rooms/$roomKey/messages`).orderByChild('createdAt').startAt('15039996197').on(//code);

这样,它将返回message内具有名为createdAt的子节点的所有节点,并从15039996197开始.对查询进行排序可能会降低性能,因为我很高兴看看.indexOn规则.

This way it'll return all nodes inside message that has a child named createdAt an it starts at 15039996197. Ordering your query may be a little bad for performance, for that i sugest taking a look at .indexOn rule.

有关更多信息,请此处.

希望这会有所帮助.

这篇关于如何在Firebase查询中使用startAt()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆