【发布时间】:2018-11-20 15:59:38
【问题描述】:
我正在使用 firebase 构建数据库应用程序。我正在尝试获取特定文档的 firebase 生成的 id。该文档作为 Promise 返回,它返回一个 QuerySnapshot。 forEach 循环将每个 QueryDocumentSnapshot 拉出 QuerySnapshot,因此我正在使用 QueryDocumentSnapshot 类。文档说这个类与 DocumentSnapshot 类具有相同的 API 界面。
https://developers.google.com/android/reference/com/google/firebase/firestore/QueryDocumentSnapshot
由于 DocumentSnapshot 类有一个 getId() 方法,我想我会使用它。
searchBooks(){
this.booksSearched = true;
this.bookService.getBookList(this.authorName).get().then(bookListSnapshot =>{
this.bookList = [];
bookListSnapshot.forEach( snap =>{
this.bookList.push({
authorLastName: snap.data().authorLastName,
title: snap.data().title,
edition: snap.data().edition,
id: snap.getId() <-------ISSUE HERE-----------
});
return false;
});
});
}
但是,我收到此错误。
property 'getId' does not exist on type 'QueryDocumentSnapshot'
这是 bookService 的代码
getBookList(authorLastName): firebase.firestore.Query{
return this.bookListRef.where("authorLastName", "==", authorLastName);
}
更疯狂的是当我尝试更改我的代码时。我认为我不会使用 forEach 循环将 QueryDocumentSnapshot 拉出,而是利用 bookListSnapShot 是一个 QuerySnapshot 并调用它的 getDocuments() 方法这一事实。 https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/QuerySnapshot
这将返回一个 DocumentSnapshots 列表,这可能意味着可以避免 getId() 方法的任何继承问题。但是,当我尝试时,我得到了这个错误:
var documents = bookListSnapshot.getDocuments();
Property 'getDocuments' does not exist on type 'QuerySnapshot'
对我来说,文档似乎与编辑器抛出的错误相矛盾。有谁知道怎么回事?
感谢您的阅读,
-乔尔
【问题讨论】:
标签: javascript firebase google-cloud-firestore