【问题标题】:Firebase JavaScript OrderbyFirebase JavaScript 排序
【发布时间】:2021-02-24 02:59:55
【问题描述】:

您好,以下是 Cloudstore 上的 JSON 结构。

我想获得特定用户的最后 10 条通知,所以我正在尝试以下代码。

var collectionDocument = 'b12b9ffa-eca9-4022-b6a4-497eef873100';
console.log(collectionDocument);
var docRef = database.collection(collectionDocument + "/Notifications/");
resposne = docRef.orderBy("Timestamp").limit(3);
console.log(resposne.data());

它会抛出一个错误

home:198 Uncaught TypeError: resposne.data is not a function

我做得对吗?还是我需要更改代码? Reference

【问题讨论】:

  • 您的代码正在使用 Firestore,但您的屏幕截图显示的是实时数据库。它们是具有不同功能和 API 的不同数据库。您无法从 Firestore 代码中查询 RTDB。

标签: javascript firebase google-cloud-firestore


【解决方案1】:

您应该使用异步等待。在发出请求的函数声明中附加 async 关键字。然后你需要在你的response 对象上调用get() 方法。

var docRef = database.collection(collectionDocument + "/Notifications/");
const response = docRef.orderBy("Timestamp").limit(3);
const data= await response.get();

现在您将能够访问数据对象。此外,我认为您必须遍历 docs 属性才能访问有关每个单独文档的数据,如下所示

data.docs.forEach(item=>console.log(item.data()));

【讨论】:

  • 不需要异步
  • 您在响应对象上调用了错误的方法。您需要调用 get() 方法而不是 data() 方法
猜你喜欢
  • 1970-01-01
  • 2017-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-03
  • 1970-01-01
  • 2018-01-19
相关资源
最近更新 更多