无法从Firestore数据库 v9 中删除文档

huangapple go评论65阅读模式
英文:

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);
  };

huangapple
  • 本文由 发表于 2023年2月18日 17:16:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定