英文:
Not able to delete document from firestore database v9
问题
以下是您要翻译的内容:
"users" 集合中包含一个 "userID" 文档,在该文档内又包含另一个 "favourite" 集合,而在这个集合中,存储着 "favouriteId" 的数据。
所以我想要删除 "favourite" 集合内的数据。
我的数据库结构如下:
users(集合)>userID(文档)>favourite(集合)>favouriteId(文档)
以下是我用于此操作的代码:
const deleteDoc = async (id) => {
const userRef = collection(db, "users");
const favRef = collection(
userRef,
`${firebaseAuth?.currentUser?.uid}/favourite`
);
const favId = doc(favRef, id);
await deleteDoc(favId);
};
favouriteId=id,
userId=firebaseAuth?.currentUser?.uid
我使用的是 Firebase v9。
英文:
There is a collection users
and inside user there is a document userID
and inside this document there is another collection favourite
and inside this collection , there is id's of saved favourite data favouriteId
.
So i want to delete that data inside favourite
colletion.
My db look likes:-
users(collection)>userID(document)>favourite(collection)>favouriteId(document)
Here is my code for that:
const deleteDoc = async (id) => {
const userRef = collection(db, "users");
const favRef = collection(
userRef,
`${firebaseAuth?.currentUser?.uid}/favourite`
);
const favId = doc(favRef, id);
await deleteDoc(favId);
};
favouriteId=id ,
userId=firebaseAuth?.currentUser?.uid
I use firebase v9.
答案1
得分: 1
你的代码大部分是正确的,你只需要对代码进行一些更改,比如这样:
const deleteDocu = async (userId, favouriteId) => {
const userRef = collection(db, "users");
const favRef = collection(userRef, userId, "favourite");
const favId = doc(favRef, favouriteId);
await deleteDoc(favId);
};
英文:
your code is Most of correct you just need some changings in code like that
const deleteDocu = async (userId, favouriteId) => {
const userRef = collection(db, "users");
const favRef = collection(userRef, userId, "favourite");
const favId = doc(favRef, favouriteId);
await deleteDoc(favId);
};
答案2
得分: 0
你的函数命名方式与Firebase函数相同。
只需更改函数名称:
const deleteDocu = async (id) => {
const userRef = collection(db, "users");
const favRef = collection(
userRef,
`${firebaseAuth?.currentUser?.uid}/favourite`
);
const favId = doc(favRef, id);
await deleteDoc(favId);
};
英文:
You are naming your function in the same way as the Firebase function.
Just change the function name
const deleteDocu = async (id) => {
const userRef = collection(db, "users");
const favRef = collection(
userRef,
`${firebaseAuth?.currentUser?.uid}/favourite`
);
const favId = doc(favRef, id);
await deleteDoc(favId);
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论