如何从vuejs和firestore获取文档的ID? [英] How get the id of an document from vuejs and firestore?
本文介绍了如何从vuejs和firestore获取文档的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码可从Firestore获取所有文档:
i have this code to get all of documents from firestore:
const getThemesList = async ({ commit }) => {
const snapshot = await firebase
.firestore()
.collection('themes')
.get();
const promiseThemes = snapshot.docs.map(doc => doc.data());
commit(types.GET_THEMES, promiseThemes);
};
我可以列出该功能,每个项目都有一个删除按钮:
by that fuction i can listing, each item have a delete button:
<v-btn text @click="deleteTheme(item)">
Delete
</v-btn>
每个项目仅具有以下参数:
each item only have these parameters:
{
description: (...)
name: (...)
type: (...)
}
但是,如果我没有他们的ID,该如何删除项目?
but how can i delete an item, if i dont have their id?
db.collection('themes')
.doc(theme.id)
.delete()
.then(function() {
我如何获得身份证?theme.id?
how can i get the id? theme.id?
我对@DougStevenson的答案有误
Edit 1: i have this wrong with @DougStevenson's answer
推荐答案
doc
is a DocumentSnapshot that has an id
property. Just add the document ID into each item object that you generate:
const promiseThemes = snapshot.docs.map(doc => {id: doc.id, ...doc.data());
现在您有了一个具有 id
属性的对象,可用于删除文档.
Now you have an object with an id
property that you can use to delete the document.
这篇关于如何从vuejs和firestore获取文档的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文