Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

204
Vistas
How to delete parent key by its value in firebase using javascript

So I'm a newbie in using firebase real time database and I can't delete products key child with id specified.

enter image description here

I've tried this approach but it didn't yield any success:

removeProductById = async (id, user) => {
    const userProductRef = db.ref(`products/product`);
    try{
        var query = userProductRef.orderByChild('id').equalTo(id);
        query.remove()             
        })

    }catch(error){
        console.log(`The error has occured ${error.message}`)
    }
}

So I wanna know how can one delete any tipe of key by whatever value they specify. Thank you in advance, you have always helped me guys.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There are two problems here:

  1. Your query doesn't match the nodes you want delete.
  2. Firebase doesn't support delete-queries.

Your query doesn't match the nodes you want delete

Your query starts at a node products/product, which doesn't exist in your database, and then orders/filters the id property of each child node.

What you want instead is to start at the products node, and then order/filter on its product/id property of each child node:

In code:

const userProductRef = db.ref(`products`);
var query = userProductRef.orderByChild('product/id').equalTo(id);

Don't forget that you'll need to define an index for product/id for this on the products level in your rules.

Firebase doesn't support delete-queries

You can't simply pass the query and delete instruction to Firebase. To write a node in the database, you'll need to know the entire path to that node.

So you'll need to:

  1. Execute the a query.
  2. Loop over the results in your application code.
  3. Delete each matching node individually or in a multi-path update.

With the query above, that'd be:

query.once("value").then((snapshot) => {
  snapshot.forEach((child) => {
    child.ref.remove();
  });
})

Or with a multi-path/batch update:

query.once("value").then((snapshot) => {
  let updates = {};
  snapshot.forEach((child) => {
    updates[child.key] = null;
  });
  ref.update(updates);
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda