Javascript 将documentId存储为文档中的字段

Javascript 将documentId存储为文档中的字段,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,我想将集合中创建的documentId作为字段/属性存储为同一文档中的docid。我正在使用CloudFireStore 显示错误“ReferenceError:未定义文档” 每当我尝试存储doc.id时 db.collection("Colleges").doc(user.uid).collection('Internships').doc().set({ CompanyName :this.I_company, InternshipTitle :

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


显示错误“ReferenceError:未定义文档” 每当我尝试存储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
})

实际上,不带任何路径调用a的方法将返回带有“自动生成的唯一ID”的

但您必须执行以下操作:首先获取新的
DocumentReference
,然后使用此
DocumentReference
上的方法

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。

存储文档id时收到什么错误?“ReferenceError:doc未定义”我将此作为错误获取