const db = admin.firestore();
exports.aggregateRatings = functions.firestore
.document('destinations/{destId}/reviews/{reviewId}')
.onWrite(event => {
const reviewRating = event.data.get('reviewRating');
const destinationId = event.params.destId;
const destRef = db.collection('destinations').doc(destinationId);
return db.transaction(transaction => {
return transaction.get(destRef).then(destDoc => {
const numberOfReviews = destDoc.data('numberOfReviews') + 1;
var oldRatingTotal = destDoc.data('destinationRating') * destDoc.data('numberOfReviews');
var newAvgRating = (oldRatingTotal + reviewRating) / numberOfReviews;
return transaction.update(destRef, {
destinationRating: newAvgRating,
numberOfReviews: numberOfReviews
});
});
});
});
当调用firebase函数时,我收到此错误,但我不知道为什么会发生这种情况。有人可以帮我吗?任何帮助将不胜感激!