Jer*_*gle 1 javascript google-cloud-firestore
我正在将 React 与云 Firestore 结合使用,我试图添加自己的文档 ID,而不是自动生成的文档 ID。如果我们不能这样做,有没有办法获得回调来获取提交数据时创建的文档ID?
firebase.firestore()
.collection("user5d")
.document("custom-id")
.add({
urn: randomNumber,
line1: "some text",
line2: "line 2 text",
slashColor: "green",
textColor: "white",
});
Run Code Online (Sandbox Code Playgroud)
为了澄清这些方法之间的差异:
.add() -> 使用自动生成的文档 ID 在集合中添加文档。您只能在一个上使用它CollectionReference。例如:
firebase.firestore().collection("colName").add({...})
Run Code Online (Sandbox Code Playgroud)
.set() -> 在集合中创建一个新文档,但与 不同的是.add(),您需要指定文档 ID。该方法只能在 a 上调用DocumentReference。例如:
firebase.firestore().collection("colName").doc("docID").set({...})
Run Code Online (Sandbox Code Playgroud)
.update() -> 更新集合中的现有文档。您需要指定文档 ID。与 类似.set(),该方法只能在 a 上调用DocumentReference。例如:
firebase.firestore().collection("colName").doc("docID").update({...})
Run Code Online (Sandbox Code Playgroud)
您也可以使用它.set()来更新文档,但它会覆盖其余字段,除非您在SetOptions中指定合并属性,例如.set({...}, {merge: true})
| 归档时间: |
|
| 查看次数: |
955 次 |
| 最近记录: |