I’ve got a little problem with my runTransactions
using the Cloud Functions (JavaScript).
My app has a startTime
, completeTime
and deleteTime
. So when the time reaches “completeTime”, it adds +1 to a field in the Firestore as shown in the CODE . This works well.
CODE
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const database = admin.firestore();
const min = "*/5 * * * *";
exports.scheduled = functions.pubsub.schedule(min).onRun(async (context) => {
const batch = database.batch();
const {docs} = await database.collection("ADSGHANA").get();
docs.map((doc) => {
const {
AdsId,
UID,
TimestampArrival,
TimestampDeparture,
PostStatus,
TimeToDelete,
} = doc.data();
const getDriveInfo = database.collection("USERS GHANA").doc(UID);
const now = admin.firestore.Timestamp.now();
const starting = now > TimestampDeparture;
const ending = now > TimestampArrival;
const deleting = now > TimeToDelete;
let val = PostStatus;
if (starting && PostStatus !== "Cancelled") val = "Started";
if (ending && PostStatus !== "Cancelled") val = "Completed";
batch.update(
database.doc(`ADSGHANA/${doc.id}`), {
PostStatus: val,
},
);
if (starting && PostStatus !== "Cancelled") {
database.collection("USERS GHANA")
.doc(UID).collection("USER JOURNEY")
.doc(UID).collection("AS A DRIVER")
.doc(AdsId).update({"PostStatus": "Started"});
database.doc(`ADSGHANA/${doc.id}`)
.collection("GHANA REQUEST")
.where("AdStatus", "not-in",
["Passenger cancelled", "Rejected", "Driver cancelled"])
.get()
.then((value) => {
value.docs.forEach((element) => {
database.doc(`ADSGHANA/${doc.id}`)
.collection("GHANA REQUEST").doc(element.id)
.update({"AdStatus": "Started"});
database.collection("USERS GHANA")
.doc(UID)
.collection("USER JOURNEY")
.doc(UID)
.collection("AS A DRIVER")
.doc(AdsId)
.collection("USER REQUEST")
.where("AdStatus", "not-in",
["Passenger cancelled", "Rejected", "Driver cancelled",
"Passenger Cancelled", "Driver Cancelled",
])
.get()
.then((value) => {
value.docs.forEach(
(element) => {
database.collection("USERS GHANA")
.doc(UID)
.collection("USER JOURNEY")
.doc(UID)
.collection("AS A DRIVER")
.doc(AdsId)
.collection("USER REQUEST")
.doc(element.id)
.update({"AdStatus": "Started"});
});
});
database.collection("USERS GHANA")
.doc(element.id)
.collection("USER JOURNEY")
.doc(element.id)
.collection("AS A PASSENGER")
.doc(AdsId)
.update({"AdStatus": "Started"});
});
});
} null;
if (ending && PostStatus !== "Cancelled") { //This is the completeTime
database.runTransaction(async (transaction) => {//This is where it adds +1
const driverInfo = await transaction.get(getDriveInfo);
const driverCompleted = driverInfo.get("DrivRideCompleted") + 1;
transaction.update(getDriveInfo,
{"DrivRideCompleted": driverCompleted});
});
database.collection("USERS GHANA")
.doc(UID).collection("USER JOURNEY")
.doc(UID).collection("AS A DRIVER")
.doc(AdsId).update({"PostStatus": "Completed"});
database.collection("USERS GHANA")
.doc(UID)
.collection("USER JOURNEY")
.doc(UID)
.collection("AS A DRIVER")
.doc(AdsId)
.collection("USER REQUEST")
.where("AdStatus", "not-in",
["Passenger cancelled",
"Rejected",
"Driver cancelled",
"Driver Cancelled", "Passenger Cancelled"],
)
.get()
.then((value) => {
value.docs.forEach(
(element) => {
database.collection("USERS GHANA")
.doc(UID)
.collection("USER JOURNEY")
.doc(UID)
.collection("AS A DRIVER")
.doc(AdsId)
.collection("USER REQUEST")
.doc(element.id)
.update(
{"AdStatus": "Completed"},
);
},
);
});
database.collection("ADSGHANA").doc(UID)
.collection("GHANA REQUEST")
.where("AdStatus", "not-in",
["Passenger cancelled", "Rejected", "Driver cancelled",
"Passenger Cancelled"])
.get().then((value) => {
value.docs.forEach((element) => {
const getPassInfo = database.collection("USERS GHANA")
.doc(element.id);
database.runTransaction(async (transaction) => {
const passInfo = await transaction.get(getPassInfo);
const passCompleted = passInfo.get("PassRideCompleted") + 1;
transaction.update(getPassInfo,
{"PassRideCompleted": passCompleted});
});
database.doc(`ADSGHANA/${doc.id}`)
.collection("GHANA REQUEST").doc(element.id)
.update({"AdStatus": "Completed"});
database.collection("USERS GHANA").doc(element.id)
.collection("USER JOURNEY")
.doc(element.id)
.collection("AS A PASSENGER")
.doc(AdsId)
.update({"AdStatus": "Completed"});
});
});
} null;
if (deleting) { //When it gets to this time it adds another +1 which shouldn't happen
database.collection("ADSGHANA").doc(UID)
.collection("GHANA REQUEST")
.get().then((value) => {
value.forEach((element) => {
database.collection("ADSGHANA").doc(UID)
.collection("GHANA REQUEST").doc(element.id).delete();
});
}).then(() => {
database.collection("ADSGHANA").doc(UID).delete();
});
} null;
});
await batch.commit();
});
However, when it gets to “deleteTime”, it also add +1 to the same field making it a total of +2. I don’t understand why it’s happening
The whole app is like this (Using Flutter):
A user has a “UserCollection” called “USERS GHANA” with different fields including “DrivRideCompleted”. The user makes a post to create a new collection called “ADSGHANA”. This collection has “startTime”, “completeTime”, “deleteTime” and “Status.
So using cloud functions, when the time now = admin.firestore.Timestamp.now()
is equal to the “startTime”, it updates the field “Status” of the “ADSGHANA” collection to “Started. Works as expected.
When “now” is equal to “completeTime”, it updates the field “Status” of the “ADSGHANA” collection to “Completed” and then updates the field “DrivRideCompleted” of the collection “USERS GHANA” to +1. This works as expected.
When “now” is equal to “deleteTime”, it deletes the collections “ADSGHANA”. This also works well. However, it also adds another +1 to the field “DrivRideCompleted” of collection “USERS GHANA” again making it a total of +2. This is not what I want it to do.
What’s wrong with my code. Please check and correct it for me. I would be grateful if this you did. Thanks.