Firebase firestore触发的java脚本google函数代码无法正常工作

Firebase firestore触发的java脚本google函数代码无法正常工作,firebase,google-cloud-functions,Firebase,Google Cloud Functions,当用户在我的android应用程序上添加他们对餐馆的评论时,我将使用谷歌云功能来计算平均评级 我想做的是: 当用户单击提交按钮时,应将评级添加到firestore。然后谷歌云功能应该被触发。 此函数计算平均额定值并更新数据 我编写了下面的代码,但效果不好 我希望平均评分是平均的,但它被上次用户评论的分数所取代 //编辑问题 //问题是console.log(旧的numRatings)和consold.log(旧的ratingtotal)不正确 始终为零,,,即使实际数据不是零 下面是Androi

当用户在我的android应用程序上添加他们对餐馆的评论时,我将使用谷歌云功能来计算平均评级

我想做的是: 当用户单击提交按钮时,应将评级添加到firestore。然后谷歌云功能应该被触发。 此函数计算平均额定值并更新数据

我编写了下面的代码,但效果不好

我希望平均评分是平均的,但它被上次用户评论的分数所取代

//编辑问题

//问题是console.log(旧的numRatings)和consold.log(旧的ratingtotal)不正确 始终为零,,,即使实际数据不是零

下面是Android应用程序代码

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.btn_submit :
                if(String.valueOf(mEtxTitle.getText()) != "null"
                && String.valueOf(mEtxContent.getText()) != "null"){
                    submit();

    }

private void submit() {

        if(retrieveDataFlag){
            setVars();

            final DocumentReference documentReference = db.collection("restaurants").document(mPlace.getId());

            // add default restaurant info to document
            updateRestaurant(documentReference, restaurant);

            // add location to document
            addLocation(documentReference, mGeoPoint);

            // add user review to document
            addReview(documentReference, review, rating);

            // upload photo and download url and add it to document
            if(mImageUris != null){
                addPhoto(storageRef, documentReference, mImageUris);
            }
            finish();

        }else{
            Toast.makeText(this,"No Place Data retrived", Toast.LENGTH_SHORT).show();
        }
    }
以下是餐厅课程

@IgnoreExtraProperties
public class Restaurant {

    public String name;
    public int numRatings;
    public double avgRating;
    public String id;
    public String address;
    public String website_uri;
    public String phone_number;
    public String opening_hours;
    public String price_level;
    public @ServerTimestamp Timestamp timestamp;

...
...

}
这是日志 函数触发两次,但avgRating和numRatings值为“0”


请详细说明具体问题,而不是说它工作不正常。这使得帮助变得更容易。特别是,您在云功能控制台中是否遇到任何错误?仅供参考,我已经测试了您的云功能,它可以正常工作。是否确实使用正确的格式(数字)写入了正确的数据?我添加了console.log,发现restDoc.data().numRatings和restDoc.data().avgRating始终是使用类型编号存储的“0”数据。没有错误代码,在日志中,函数完成状态为:“ok”,请指定确切的问题,而不是说它工作不正常。这使得帮助变得更容易。特别是,您在云功能控制台中是否遇到任何错误?仅供参考,我已经测试了您的云功能,它可以正常工作。是否确实使用正确的格式(数字)写入了正确的数据?我添加了console.log,发现restDoc.data().numRatings和restDoc.data().avgRating始终是使用类型编号存储的“0”数据。并且没有错误代码,在日志中,函数已完成,状态为“ok”
    private void addReview(final DocumentReference restaurantRef, final Map<String, String> review, final Map<String, Float> rating){
        restaurantRef.collection("reviews").document().set(review);
        restaurantRef.collection("ratings").document().set(rating);
    }
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

const db = admin.firestore();
// [START_EXCLUDE]
const settings = {timestampsInSnapshots: true};
db.settings(settings);
// [END_EXCLUDE]

// [START aggregate_function]
exports.aggregateRatings = functions.firestore
    .document('restaurants/{restId}/ratings/{ratingId}')
    .onWrite((change, context) => {
      // Get value of the newly added rating
      var ratingVal = change.after.data().rating;
      console.log(ratingVal)

      // Get a reference to the restaurant
      var restRef = db.collection('restaurants').doc(context.params.restId);

      // Update aggregations in a transaction
      return db.runTransaction(transaction => {
        return transaction.get(restRef).then(restDoc => {
          // Compute new number of ratings
          var newNumRatings = restDoc.data().numRatings + 1;
          console.log("resDoc", restDoc.data())
          console.log("old numRatings : ", restDoc.data().numRatings)
          console.log("new numRatings : ", newNumRatings)

          // Compute new average rating
          var oldRatingTotal = restDoc.data().avgRating * restDoc.data().numRatings;
          console.log("oldRatingTotla : " ,oldRatingTotal)

          var newAvgRating = (oldRatingTotal + ratingVal) / newNumRatings;
          console.log("new AvgRating : ", newAvgRating)

          // Update restaurant info
          return transaction.update(restRef, {
            avgRating: newAvgRating,
            numRatings: newNumRatings
          });
        });
      });
    });
// [END aggregate_function]