javascript - 根据时间戳按降序排列 Firebase 查询

标签 javascript firebase react-native firebase-realtime-database nosql

我在使用 React Native 从 firebase 和 javascript 返回按降序排列的对象数组时遇到问题。我本质上是“加入”两个 firebase parent ,然后将它们插入一个数组。

async loadChats(userId){

 this.chatIdRef = firebase.database().ref("members");
 this.chatIdRef.off();
 this.chatsRef = firebase.database().ref("chats");
 this.chatsRef.off();

 items = [];
 this.chatIdRef.child(userId).on('child_added', snap => {
 this.chatsRef.child(snap.key).startAt().on('value', snapshot => {

    items.push({
      receiverId: snap.val().userId,
      timeStamp: snapshot.val().createdAt,
      name: snap.val().userName,
      key: snapshot.key
    });

  this.setState({
    dataSource: this.state.dataSource.cloneWithRows(items)
  });

  });
 });
}

我尝试在查询中使用“orderByChild('createdAt')”,但没有成功。

最佳答案

根据 Firebase 文档,使用 orderByChild 的查询按以下顺序返回列表:-

Children with a null value for the specified child key come first.

Children with a value of false for the specified child key come next. If multiple children have a value of false, they are sorted lexicographically by key.

Children with a value of true for the specified child key come next. If multiple children have a value of true, they are sorted lexicographically by key.

Children with a numeric value come next, sorted in ascending order. If multiple children have the same numerical value for the specified child node, they are sorted by key.

Strings come after numbers and are sorted lexicographically in ascending order. If multiple children have the same value for the specified child node, they are ordered lexicographically by key.

Objects come last and are sorted lexicographically by key in ascending order.

因此,从上面的内容来看,请注意,当您的数据具有数值时,它会按升序顺序返回。现在您知道了检索数据的顺序,您只需对客户端应用程序进行更改即可反向显示数据(按降序排列)。也许颠倒你的列表或类似的东西?

关于javascript - 根据时间戳按降序排列 Firebase 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44954975/

相关文章:

javascript - 拉斐尔,鼠标悬停和鼠标移开

javascript - 访问指令的所有 Controller

javascript - 提取字符串部分的最有效方法

ios - iOS 设备上的 Expo.io 离线测试

reactjs - 在 React Native 中添加数据并保存状态

javascript - Hub 方法在 hub 类外部不起作用

Firebase Firestore 换行命令

swift - Firebase Firestore - 或查询替代方案

firebase - 我如何知道我的逆火集合何时已完成从服务器的加载?

android - adb 服务器版本 (39) 与此客户端 (40) 不匹配;杀...亚行服务器没有确认