使用orderByChild Firebase获取最新帖子 [英] Using orderByChild Firebase to get most recent posts

查看:61
本文介绍了使用orderByChild Firebase获取最新帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于"order_date"值对查询进行排序.不幸的是,它以相反的顺序返回它们(最早的顺序在前).我也试图通过分页来实现.这是我当前的查询.

I want to order my query based on the 'order_date' value. Unfortunately, it is returning them in the reverse order (oldest orders first). I am also trying to accomplish this with pagination. This is my current query.

var orders = fbdatabase.ref('orders').orderByChild('order_date');
orders.on('value', function(snapshot) {
  console.log(snapshot.val())
});

推荐答案

无法使用Firebase反向查询.您需要使用limitToLast:

There is no way to query in reverse order with Firebase. You'll need to use limitToLast:

var orders = fbdatabase.ref('orders')
  .orderByChild('order_date')
  .limitToLast(20);

orders.on('value', function(snapshot) {
  console.log(snapshot.val().reverse());
});

或者,您可以存储一个其他字段,如order_date_desc,该字段使用负值(假设时间戳记).

Alternatively, you could store an additional field like order_date_desc that uses a negative value (assuming timestamp).

这篇关于使用orderByChild Firebase获取最新帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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