代码之家  ›  专栏  ›  技术社区  ›  Vachi

类型错误:db。事务不是功能

  •  0
  • Vachi  · 技术社区  · 8 年前
    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 回复  |  直到 8 年前
        1
  •  1
  •   Doug Stevenson    8 年前

    您要查找的方法名称是 runTransaction (非“交易”)。

    推荐文章