Javascript 在firebase的另一个集合中存储文档的引用
我有一个collection-1,它存储文档的许多属性。我想创建另一个集合,集合引用与以前的集合具有相同的ID,并存储一个重要属性,以便更快地查找 如何在Firebase数据库中执行此操作Javascript 在firebase的另一个集合中存储文档的引用,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,我有一个collection-1,它存储文档的许多属性。我想创建另一个集合,集合引用与以前的集合具有相同的ID,并存储一个重要属性,以便更快地查找 如何在Firebase数据库中执行此操作 collection-1 |--{auto-generated-id-1} |--property1:value1 |--property2:
collection-1
|--{auto-generated-id-1}
|--property1:value1
|--property2:value2
|--peoperty3:value3
你可以。如果该文档不存在,firestore将使用您提供的id创建一个新文档。因此,在您的情况下,应该是:
collection-reference
|--{same auto-generated-id-from above}
|--peoperty3:value3
请正确区分firebase(实时)数据库和firestore。感谢您接受答案。除了将您编写的代码放在then()中,还有其他方法可以做到这一点吗?比如不筑巢?我不知道你说的筑巢到底是什么意思。。。异步等待中不涉及嵌套是。您可以使用
async-wait
或then-catch
。你知道吗,你也可以连锁然后块db.collection('c').get()。然后(()=>{return someNewPromise})。然后(()=>{return anotherPromise})。然后(()=>{do soomething})。catch(err=>{log error})
catch块应仅位于最后一个then块的末尾。必须在前一个then块的末尾返回一个承诺
,以便执行下一个then块。
db.collection("collection-reference").doc("your-uid").set({
property3: "value3"
})
.then(function() {
console.log("Document successfully written!");
})
.catch(function(error) {
console.error("Error writing document: ", error);
});