【问题标题】:Store documentId as field in document将 documentId 存储为文档中的字段
【发布时间】:2020-01-05 19:20:05
【问题描述】:

我想将在集合中创建的 documentId 作为字段/属性作为 docid 存储在同一个文档中。我正在使用 Cloud Firestore。

显示错误“ReferenceError: doc is not defined” 每当我尝试存储 doc.id 时

db.collection("Colleges").doc(user.uid).collection('Internships').doc().set({

          CompanyName :this.I_company,
            InternshipTitle : this.I_title,

            duration: this.duration,
            amount:this.I_amount,
            week:this.week,
            docid: doc.id
})

【问题讨论】:

  • 您在存储 doc id 时收到什么错误?
  • "ReferenceError: doc is not defined" 我得到这个错误

标签: javascript firebase google-cloud-firestore


【解决方案1】:

确实,在没有任何路径的情况下调用CollectionReferencedoc() 方法将返回带有“自动生成的唯一ID”的DocumentReference

但你必须这样做:首先获取新的DocumentReference,然后在这个DocumentReference上使用set()方法。

var docRef = db.collection("Colleges").doc(user.uid).collection('Internships').doc();

docRef.set({

          CompanyName :this.I_company,
            InternshipTitle : this.I_title,

            duration: this.duration,
            amount:this.I_amount,
            week:this.week,
            docid: docRef.id
})

但是,您可以仔细检查是否真的需要在文档的特定字段中写入docRef.id,因为无论如何您可以稍后使用id 属性获取它。

【讨论】:

    猜你喜欢
    • 2020-11-05
    • 2022-12-04
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 2011-03-19
    相关资源
    最近更新 更多