Home > Back-end >  FirebaseError: Function CollectionReference.doc() cannot be called with an empty path. (When i delet
FirebaseError: Function CollectionReference.doc() cannot be called with an empty path. (When i delet

Time:08-04

When I delete from table user I get this error below but I don't know how I can fix this error. Do I need to pass some function into the doc?

error

console.log browser

Here's my code I've written so far.

ColecaoCliente.ts:

import firebase from "../config"
import Cliente from "../../core/Cliente";
import ClienteRepositorio from "../../core/ClienteRepositorio";

export default class ColecaoCliente implements ClienteRepositorio {
    
    #conversor = {
        toFirestore(cliente: Cliente) {
            return {
                nome: cliente.nome,
                idade: cliente.idade,
            }
        },
        fromFirestore(snapshot: firebase.firestore.QueryDocumentSnapshot, options: firebase.firestore.SnapshotOptions): Cliente {
            const dados = snapshot.data(options)
            return new Cliente(dados.nome, dados.idade, snapshot.id)
        }
    }
   
    async salvar(cliente: Cliente): Promise<Cliente> {
        if(cliente?.id) {
           await this.colecao().doc(cliente.id).set(cliente)
           return cliente
        } else {
            const docRef = await this.colecao().add(cliente)
            const doc = await docRef.get()     
            return doc.data()      
        }
    }

    async excluir(cliente: Cliente): Promise<void> {
        return this.colecao().doc(cliente.id).delete()
    }

    async obterTodos(): Promise<Cliente[]> {
       const query = await this.colecao().get()
        return query.docs.map(doc => doc.data()) ?? []
    }

    private colecao() {
     return firebase.firestore()
     .collection('clientes')
     .withConverter(this.#conversor)
    }
}

CodePudding user response:

If looks like cliente.id is empty in your call to excluir. If that is a valid call for your use-case, you'll want to handle that condition like you do in salvar:

async excluir(cliente: Cliente): Promise<void> {
    if(cliente?.id) {
      return this.colecao().doc(cliente.id).delete()
    } else {
      ... whatever is the right behavior when there's no cliente.id
    }
}
  • Related