i'm using react and firebase. I have a simple cloud function that is not performing. I'm adding reviews to my site and i want cloud functions to add tracking information about the reviews like the number of overall reviews, unpublished reviews for both the user profile and the overall app.
Here is my code:
exports.onCreateReview = functions.firestore
.document(`reviews/{document}`)
.onCreate(async (snapshot) => {
const review = snapshot.data();
// overall reviews directory
const trackingDocRef = firestore
.collection("tracking")
.doc("reviewTracking");
// user directory
const profileDocRef = firestore.collection("profiles").doc(review.proID);
trackingDocRef.update({
reviewsAll: admin.firestore.FieldValue.increment(1),
reviewsUnpublishedNo: admin.firestore.FieldValue.increment(1),
reviewsUnpublishedRating: admin.firestore.FieldValue.increment(
review.ratingOverall
),
});
profileDocRef.update({
reviewsAll: admin.firestore.FieldValue.increment(1),
reviewsUnpublishedNo: admin.firestore.FieldValue.increment(1),
reviewsUnpublishedRating: admin.firestore.FieldValue.increment(
review.ratingOverall
),
});
});
When a review is created, the function executes and the review tracking document changes momentarily, according to the function(to the wrong value - instead of increasing it decreases), but then it changes back to zero.
When i check on cloud logging, the is an image of a bug with severity: "DEBUG" but nothing else that's useful.
The keypoints of the log entry:
The function "reviews-onCreateReview" was executed in the "us-central1" region. The function took 1451 milliseconds to complete. The function finished with a status of "ok". The runtime version of the function was "nodejs20...". The execution ID of the function was ...
Its either there is wrong changes or no changes.
What is wrong with my function?
I didn’t test your Cloud Function but you don’t manage correctly its life cycle. The following should do the trick.
I made the assumption that in
const trackingDocRef = firestore.collection("tracking").doc("reviewTracking");,firestoreis define before the function asadmin.firestore().