【问题标题】:The document is not a function? [closed]文件不是函数吗? [关闭]
【发布时间】:2018-11-13 13:12:30
【问题描述】:

我正在用 JavaScript 编写代码。我有这个 if 语句:

var rightsRef = db.collection("users").document(uid).collection("rights");
        if(createDocumentWithId(rightsRef, "readGrades", "false", null, null, null, null) === true) {
          window.location.href = "../public/main/main_index.html";
        }  

下面是应该在 cloud firestore 中添加值的函数:

function createDocumentWithoutId(var databaseRef, var titleValue1, var contentValue1, var titleValue2, var contentValue2, var titleValue3, var contentValue3) {
  databaseRef.add({
    titleValue1: contentValue1,
    titleValue2: contentValue2,
    titleValue3: contentValue3,
  }).then(function() {
    return true;
  }).catch(function(error) {
    console.error("Error adding document: ", error);
    return false;
  });
}  

但我收到此错误:

TypeError: db.collection(...).document is not a function

【问题讨论】:

标签: javascript firebase google-cloud-firestore


【解决方案1】:

我之所以这么说,是因为我实际上并没有使用 Firebase,但根据您遇到的错误,db.collection("users") 正在返回一个没有 document 属性的对象。

当我简要查看 firebase api 文档时,用于获取文档参考的 API 看起来是 doc 而不是 document,如 here 所示

另外,查看at the api docs,您可以看到集合引用只有一个名为doc 的函数。

【讨论】:

    【解决方案2】:

    db.collection("users") 返回一个CollectionReference 对象。从 API 文档中可以看出,它没有名为 document() 的方法。它确实有一个名为doc() 的方法,这正是您想要的。您可能将 Firestore JavaScript API 与 Java API 混淆了。

    【讨论】:

      猜你喜欢
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      • 2019-10-26
      • 2021-11-04
      • 2020-04-02
      • 2019-01-22
      • 2017-10-25
      相关资源
      最近更新 更多