![](/img/trans.png)
[英]Is it possible to snapshot an auto-generated ID document from FireStore?
[英]Take one specific auto-generated ID of a document in Cloud firestore
我是创建一个使用云 Firestore 作为数据库的简单网站的新手。 当用户添加新数据时,我让 firestore 自动生成文档 ID(如图所示)。 现在我想获得那个文档 ID,但我对此一无所知。 这是我的代码:
db.collection("todos").add({
content: input.value,
name: name.value,
time_update: time
})
.then(function(docRef) {
console.log("Document written with ID: ", docRef.id);
return documentId = docRef.id // i try to do this but it does not return the documentID outside this promise
})
.catch(function(error) {
console.error("Error adding document: ", error);
});
当我在 promise 之外使用 console.log(documentId) 时,它返回“未定义”。 请帮忙,谢谢!
您无需等待回调。 您可以立即获得随机 ID:
const docRef = db.collection("todos").doc()
const id = docRef.id
然后使用set()
添加数据:
docRef.set(...)
随机 ID 是在客户端生成的,而不是在服务器上生成的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.