简体   繁体   English

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

[英]How can I return ordered data from Firebase database with Node.js using the firebase-admin module?

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...). 我看到这两个火力点数据库调用是不同的,一个是一次然后 (因此它必须是一个承诺..?),另一个是 (所以我想这只是一个正常的回调...)。

Do you have any idea on why is happening here? 您对这里为什么会发生有任何想法吗? Sorry if this is obvious but I'm somewhat of a beginner.. 抱歉,如果这很明显,但是我有点初学者。

When you execute a query against the Firebase Database, there will potentially be multiple results. 当您对Firebase数据库执行查询时,可能会有多个结果。 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. 即使只有一个结果,快照也将包含一个结果的列表。

So in your first example snapshot holds they things: the keys of the matching nodes, their values, and the ordering between them. 因此,在您的第一个示例中, snapshot包含了它们:匹配节点的键,它们的值以及它们之间的顺序。 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. 当您调用snapshot.val()此数据将转换为常规JSON对象,该对象没有空间容纳所有三段信息。 At this point the ordering information is dropped. 此时,订购信息被删除。

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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.js 12.x AWS lambda 不使用 firebase-admin 实时数据库返回 - Node.js 12.x AWS lambda does not return using firebase-admin realtime database firebase JS SDK 包是否与云函数的 Node.js 环境兼容? 为什么要使用 firebase-admin? - Is the firebase JS SDK package compatible with the Node.js environment of a cloud function? Why use firebase-admin? 从node.js中的firebase实时数据库获取数据时如何通过函数返回数据 - how to return data through function when fetching data from firebase realtime-database in node.js Firebase-admin Firestore node.js 与模块化 v9 js sdk 共享逻辑 - Firebase-admin Firestore node.js share logic with modular v9 js sdk 如何获取插入文档的 ID firebase-admin - How can I get the id of a inserted document firebase-admin Node.js:如何获取Firebase数据库数据快照的参考 - Node.js: How can I get the reference for a Firebase Database Data Snapshot 如何将一个值仅推送一次到 firebase 数据库 (firebase-admin)? - How do I push a value only once into firebase database (firebase-admin)? 如何阅读Firebase Node.js模块? - How to read Firebase Node.js module? export.module 和 Firebase-admin 的问题 - Problem with exports.module and Firebase-admin 如何在 node.js 中获取 firebase 实时数据库的子节点值? - How can i reach child node values of firebase realtime database in node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM