如何在web javascript中从firebase数据库中获取倒数第二个值?

如何在web javascript中从firebase数据库中获取倒数第二个值?,javascript,firebase,firebase-realtime-database,Javascript,Firebase,Firebase Realtime Database,这是我检索最后一个值的代码 Firebase .database() .ref('comment/commentHistory/' + postId) .limitToLast(1) .once('value', (snapshot) => { // Stuff }); 你的帖子是连续的吗?如果是,则可以使用orderByKey子句。如果不是,您需要添加某种计数器或时间戳 Firebase .database() .re

这是我检索最后一个值的代码

Firebase
  .database()
  .ref('comment/commentHistory/' + postId)
  .limitToLast(1)
  .once('value', (snapshot) => {
    // Stuff                
  });

你的帖子是连续的吗?如果是,则可以使用orderByKey子句。如果不是,您需要添加某种计数器或时间戳

Firebase
  .database()
  .ref('comment/commentHistory')
  .orderByKey
  .limitToLast(2)
  .once('value', (snapshot) => {
      // take the last item with the lowest key in the snapshot
  });
如果您的数据不是连续的,您需要将时间戳作为子项添加到您的帖子中

然后可以使用orderByChild子句:

 Firebase
      .database()
      .ref('comment/commentHistory')
      .orderByChild('timestamp')
      .limitToLast(2)
      .once('value', (snapshot) => {
          // take the last item with the lowest timestamp in the snapshot
      });

它是最后一个按时间戳显示的字段,您应该存储一个时间戳字段并对其进行查询。