Firebase JavaScript Orderby

Firebase JavaScript Orderby,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,下面是Cloudstore上的JSON结构。 我想得到一个特定用户的最后10个通知,所以我正在尝试下面的代码 var collectionDocument = 'b12b9ffa-eca9-4022-b6a4-497eef873100'; console.log(collectionDocument); var docRef = database.collection(collectionDocument + "/Notifications/"); resposne = d

下面是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

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

您应该使用异步等待。在发出请求的函数声明中附加async关键字。然后,需要对
响应
对象调用
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()方法您的代码正在使用Firestore,但屏幕截图显示的是实时数据库。它们是具有不同功能和API的不同数据库。无法从Firestore代码查询RTDB。