nosho96ohi
@nosho96ohi

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

firebaseデータ削除に関する質問

解決したいこと

firebaseのデータベースから、今、ログインしているユーザーのドキュメントを削除したいです。

deleteDoc(doc(db, "status", documentId), where("uid", "==", this.uid))
deleteDoc(collection(db, "status", documentId), where("uid", "==", this.uid))

上のどちらもダメでした。

deleteDoc(doc(db, "status", "uid"), where("uid", "==", this.uid))

これだとエラーは消えたけどデータは消えないです。

発生している問題・エラー

n.indexOf is not a function
TypeError: n.indexOf is not a function
    at ResourcePath.fromString (webpack-internal:///./node_modules/@firebase/firestore/dist/index.esm2017.js:632:35)
    at doc (webpack-internal:///./node_modules/@firebase/firestore/dist/index.esm2017.js:6743:146)
    at Proxy.resetData (webpack-internal:///./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/views/SettingView.vue?vue&type=script&lang=js:54:124)
    at Proxy.save (webpack-internal:///./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/views/SettingView.vue?vue&type=script&lang=js:111:12)
    at _ctx.changestatus.onClick._cache.<computed>._cache.<computed> (webpack-internal:///./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/vue-loader/dist/templateLoader.js??ruleSet[1].rules[3]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/views/SettingView.vue?vue&type=template&id=14a99a4b&scoped=true:138:81)
    at callWithErrorHandling (webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:285:18)
    at callWithAsyncErrorHandling (webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:293:17)
    at HTMLButtonElement.invoker (webpack-internal:///./node_modules/@vue/runtime-dom/dist/runtime-dom.esm-bundler.js:425:82)

例)

NameError (uninitialized constant World)

statusの中で、uidが現在のロインユーザーと同じものを消したいです。

image.png

よろしくお願いします。

0

2Answer

import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
import { doc, deleteDoc } from "firebase/firestore";
import { collection, doc } from "firebase/firestore"; 

const app = initializeApp(firebaseConfig);
const db = getFirestore(app);

// データベースにstatusコレクションを問い合わせる
const colRef = collection(db, "status");
// そのコレクション内の特定のdocのリファレンスをIDで取ってくる
// document_idは 5ckjg2VzFW0j6N4JAk0p 的なもの
const docToBeDeleted = doc(colRef, document_id);

// 特定のdocを削除する
await deleteDoc(docToBeDeleted);
1Like

↓これでどうでしょうか。

var query = db.collection('status').where("uid", "==", this.uid);
query.get().then(function(querySnapshot) {
  querySnapshot.forEach(function(doc) {
    doc.ref.delete();
  });
});
0Like

Comments

  1. @nosho96ohi

    Questioner

    ありがとうございます。しかし、TypeError:doc.ref.delete is not a function at evalとエラーが出ます。

  2. ↓これならどうでしょうか。

    var query = db.collection('status').where("uid", "==", this.uid);
    query.get().then(function(querySnapshot) {
      querySnapshot.forEach(function(doc) {
        deleteDoc(doc);
      });
    });
    
  3. @nosho96ohi

    Questioner

    エラーなくなりました!ありがとうございます。

  4. 解決であれば、当Q&Aをクローズしてください。

Your answer might help someone💌