TypeError:db.transaction不是函数

时间:2018-04-16 20:49:39

标签: node.js firebase google-cloud-firestore google-cloud-functions

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函数时,我收到此错误,但我不知道为什么会发生这种情况。有人可以帮我吗?任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

您正在寻找的方法名称是runTransaction(而不是“交易”)。