【问题标题】:QueryDocumentSnapshot seems to lack the methods of its parent classQueryDocumentSnapshot 似乎缺少其父类的方法
【发布时间】: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


    【解决方案1】:

    您正在使用 JavaScript 编写代码,但您正在查看使用 Java 的 Android 的 API 文档。以下是 DocumentSnapshotQueryDocumentSnapshot 的 JavaScript API 文档。 DocumentSnapshot 有一个您正在寻找的名为 id 的属性。

    【讨论】:

    • 哎呀,尴尬。谢谢!
    猜你喜欢
    • 2019-04-29
    • 1970-01-01
    • 2019-01-07
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    相关资源
    最近更新 更多