Search code examples
angularangularfire

I'm using @angular/fire/database. How do I remove or update a certain item?


I could be wrong, but the documentation is very vague. I will first explain how I am entering data and listing them.

async inserir(guiaQuestao: GuiaQuestao){

    const refGuiaQuestoes = ref(this.db, 'guiaQuestoes');

    return push(refGuiaQuestoes, {
        questao: guiaQuestao.questao,
        questaoResumida: guiaQuestao.questaoResumida
    })
  }

 listar(){
    const questoesRef = ref(this.db, 'guiaQuestoes');
    return listVal(questoesRef);
  };

I checked the @angular/fire documentation, questions already asked here and discussions in the official repository. I hope someone here can help me update and remove items in the @angular/fire Database.


Solution

  • I would like to inform you that I finally managed it with the help of ChatGPT. I had to correct it about 10 times each time an error appeared, but this way it does what I need:

    list(questoesRef).pipe(
        map((questoes) => {
            // Para cada snapshot, obtenha a chave e o valor associado
            return questoes.map((questao) => {
                const data = questao.snapshot.val(); // Valor do item
                const key = questao.snapshot.key; // Chave do item
                return { ...data, key }; // Adiciona a chave ao objeto
            });
        }
    )
    

    I used ChatGPT 3.5 and several times it gave me outdated information, such as methods that no longer exist in @angular/fire/database. But if it weren't for him, I believe I wouldn't have made it.