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

178
Vistas
Cloud Functions: Realtime Database transaction not working with condition

I am trying to decrease the field amount which represents the amount of products available for sale.

My database is structured like below:

- Stock
 - StoreId
  - DepartmentId
   - ProductId
    + price
    + amount

My storeId has a fixed value and I am only looping through the variables DepartmentId and ProductId.

Below is how I created a node array or children array for the transaction:

  const childArray = [];
  depIds.forEach(depId => {
    const prodIds = groupedProducts[depId].reduce((acc, val) => [...acc, val.prodId], []);
    prodIds.forEach(prodId => {

      childArray.push(admin.database().ref("Stock")
        .child(storeId)
        .child(depId)
        .child(prodId)
        .child("amount")
        .once('value'));
    });
  });

When I add the condition: amount > 0, nothing happens. It only works when I remove the condition.

const snapshots = await Promise.all(childArray);
snapshots.forEach(snapshot => {
  snapshot.ref.transaction(amount => {
    //if (amount) return amount + admin.database.ServerValue.increment(-1);
    if (amount > 0) return amount - 1;
        
  })
})

Furthermore, what I really need is to compare the amount in the database with the amount coming from the request (let's call it qty)

For this, I would have to retrieve the current path for productId and departmentId. But as showed below, snapshot.ref.parent does not return the path.

snapshots.forEach(snapshot => {
  snapshot.ref.transaction(amount => {
    //if (amount > 0 & amount >= qty) return amount + admin.database.ServerValue.increment(-qty);

    const productPath = snapshot.ref.parent;
    const departmentPath = snapshot.ref.parent.parent;
    // Get qty based on productPath and departmentPath
    if (amount > 0 & amount >= qty) return amount - qty;
        
  })
})

So, I have three questions:

  1. How to retrieve the current paths for departmentId and productId
  2. How to update the field amount based on a condition and abort the whole transaction if any condition is not satisfied
  3. How to make admin.database.ServerValue.increment(-qty) work

I appreciate any help!

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

0

You don't need a transaction for this. Instead, once you know the full path of the node(s) to update, you can use increment() and then use security rules to validate that the value doesn't go below 0.

So in code that'd be:

admin.database().ref("Stock")
  .child(storeId)
  .child(depId)
  .child(prodId)
  .update({ "amount", admin.database.ServerValue.increment(-1));

And then in the rules:

{
  "rules": {
    "Stock": {
      "$storeId": {
        "$depId": {
          "$prodId": {
            "amount": {
              ".validate": "newData.val() >= 0"
            }
          }
        }
      }
    }
  }
}
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