Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!