I am trying to get the orderid that I have stored on the Realtime Database of Firebase. For getting the data I have been using the firebase.database().transaction() method to avoid use conflicts between realtime use.
All I am trying to do is that get the id to assign it to the current order, then increment it by +1, and then return it to the firebase again. But the problem is that whenever I try to assign that value through the transaction method, I get a null or undefined object.
It does increment the value stored on firebase, but doesn't give an appropriate value to assign to the order object.
So, what can I do to get this done more efficiently?
here's the code I have been using to get the job done:
await firebase.database().ref().child("Order_Id").transaction((value) = > {
//Sending data to firebase to book the order
firebaseRef.child(value).set({
Appliance_Name: item.title,
Service_Requested: orderData.serviceType,
Image: orderData.uploadedImage,
User_Problem: orderData.problem,
Location: orderData.latitude + " , " + orderData.longitude,
Status: "Booked",
Time: dateTime,
Assign: ""
});
return value + 1;
});
It seems that you want to write to two different nodes of your RTDB in a transaction mode: firebase.database().ref().child("Order_Id") and firebaseRef.child(value).
Therefore the transaction needs to apply on a common ancestor of these two nodes. As explained in the doc, the update function (i.e. (value) = > {} in your case) needs to takes the current state of the data (value) as an argument and returns the new desired state you would like to write.", i.e. the new value of the data tree for this common ancestor node.