如何使用firebase-admin模块使用Node.js从Firebase数据库返回有序数据?

时间:2022-02-14 20:40:48

I'm trying to query Firebase database to get data ordered by timestamp.

我正在尝试查询Firebase数据库以获取按时间戳排序的数据。

1: This works but the data returned is not ordered by timestamp:

1:这有效,但返回的数据不按时间戳排序:

router.get('/articles', function(req, res, next) {
    admin.database().ref('articles').orderByChild('timestamp').once('value').then(function (snapshot) {
        let articles = snapshot.val();
        console.log(articles);
        res.render('articles', articles);
    });
});

2: This returns me the data ordered by timestamp like I want (I can see it in the console.log), but I get this error:

2:这会返回我想要的按时间戳排序的数据(我可以在console.log中看到它),但是我收到此错误:

// /node_modules/express/lib/response.js:1003
//   if (err) return req.next(err);
//                      ^
// TypeError: req.next is not a function

router.get('/articles', function(req, res, next) {
    admin.database().ref('articles').orderByChild('timestamp').on('child_added', function (snapshot) {
        let articles = snapshot.val();
        console.log(articles);
        res.render('articles', articles);
    });
});

I don't get what I'm doing wrong. I see that the two firebase database calls are different, one is an once and then (so it must be a promise..?) and the other is a on (so i suppose it's just a normal callback...).

我不知道我做错了什么。我看到两个firebase数据库调用是不同的,一个是一次然后(所以它必须是一个承诺......?)而另一个是on(所以我想它只是一个正常的回调......)。

Do you have any idea on why is happening here? Sorry if this is obvious but I'm somewhat of a beginner..

你对这里发生的原因有什么看法吗?对不起,如果这是显而易见的,但我有点初学者..

1 个解决方案

#1


2  

When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

对Firebase数据库执行查询时,可能会有多个结果。因此快照包含这些结果的列表。即使只有一个结果,快照也会包含一个结果的列表。

So in your first example snapshot holds they things: the keys of the matching nodes, their values, and the ordering between them. When you call snapshot.val() this data is converted into a regular JSON object, which doesn't have space for all three pieces of information. At this point the ordering information is dropped.

因此,在您的第一个示例中,快照包含它们:匹配节点的键,它们的值以及它们之间的顺序。当您调用snapshot.val()时,此数据将转换为常规JSON对象,该对象没有空间容纳所有三条信息。此时,订购信息被删除。

The solution is to use snapshot.forEach() to loop over the matching nodes in the correct order.

解决方案是使用snapshot.forEach()以正确的顺序循环匹配的节点。

admin.database().ref('articles').orderByChild('timestamp').once('value').then(function (snapshot) {
  var articles = [];
  snapshot.forEach(function(articleSnapshot)
    articles.push(snapshot.val());
  });
  console.log(articles);
  res.render('articles', articles);
});

#1


2  

When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

对Firebase数据库执行查询时,可能会有多个结果。因此快照包含这些结果的列表。即使只有一个结果,快照也会包含一个结果的列表。

So in your first example snapshot holds they things: the keys of the matching nodes, their values, and the ordering between them. When you call snapshot.val() this data is converted into a regular JSON object, which doesn't have space for all three pieces of information. At this point the ordering information is dropped.

因此,在您的第一个示例中,快照包含它们:匹配节点的键,它们的值以及它们之间的顺序。当您调用snapshot.val()时,此数据将转换为常规JSON对象,该对象没有空间容纳所有三条信息。此时,订购信息被删除。

The solution is to use snapshot.forEach() to loop over the matching nodes in the correct order.

解决方案是使用snapshot.forEach()以正确的顺序循环匹配的节点。

admin.database().ref('articles').orderByChild('timestamp').once('value').then(function (snapshot) {
  var articles = [];
  snapshot.forEach(function(articleSnapshot)
    articles.push(snapshot.val());
  });
  console.log(articles);
  res.render('articles', articles);
});