解析云代码基于Review(对象)生成列表

时间:2015-07-29 23:48:17

标签: javascript parse-platform

我正在努力解决Parse Cloud代码问题。我正在尝试根据用户评论创建啤酒厂列表。我想我需要计算每个啤酒厂的平均评价,然后用这个值来生成清单。我想只包括平均4或更高的啤酒厂。

请原谅我对此事的无知,谢谢!

这就是我所拥有的,但它无法发挥作用。

    Parse.Cloud.afterSave("reviewAvergae", function(request){
    var Review = Parse.Object.extend("Reviews");
    var query = new Parse.Query("Reviews");
    query.get(request.object.get("rating").id, {
        success: function(reviews) {
            var sum = 0;
            for (var i = 0; i < reviews; i++) {
                sum += reviews[i].get("rating");
            }
            var ave = sum / reviews.length;
            reviews.set("average", ave);
            reviews.save();
        }
    });
/*
    query.equalTo("breweryId", request.params.brewId);
    query.find({
        success: function(result) {
            var sum = 0;
            for (var i = 0; i < result.length; i++) {
                sum += result[i].get("rating");

            }
            Review.set("averageRating", sum / result.length);
            Review.save();
            console.log(sum / result.length);
        }
    });
*/
});
Parse.Cloud.define("featured", function(request, response) {


  var query = new Parse.Query("Reviews");
//   query.near("location", request.params.loc);
  query.withinMiles("location", request.params.loc, 50);
  query.limit(10);

  query.find({
      success: function(results) {
          var sum = 0;
          var rating;
          var ratings = new Array();
          var id;
          var ids = new Array();

          for (var i = 0; i < results.length; i++) {

              id = results[i].get("breweryId");
              ids.push(id);
              }





          response.success(results);
           console.log(ratings.length);




      },
      error: function() {
          response.error("No breweriers in your area have been reviewed.");
      }

  });

});

在解析时,我的Review对象如下所示:

Review Object on Parse

注意:我的实际应用适用于iOS并使用swift编写。

更新:添加brewery对象后,我尝试了以下操作。我现在收到以下错误:[错误]:找不到函数(代码:141,版本:1.7.5)

Parse.Cloud.afterSave("Reviews", function(request){
    var Review = Parse.Object.extend("Reviews");
    var breweryId = Review.get("breweryId");

    var query = new Parse.Query("Reviews");
query.equalTo("breweryId", breweryId);
query.find({
    success: function(results) {
        var total = 0;
        for (var i = 0; i < results.length; i++) {
            var object =  results[i];
            total += object.get("rating");
        }
        var averageRating = total / results.length;

        console.log(averageRating);

        var query = new Parse.Query("Brewery");
        query.equalTo("breweryId", breweryId);



    }, error: function() {

    }
  });

});

这里是我在Parse中的对象的一些更新图像

Brewery Object

Reviews Object

2 个答案:

答案 0 :(得分:2)

从评论中了解到存在具有Brewery对象的外部系统。为了对啤酒厂进行parse.com计算,有必要将它们内部表示为解析,即使它基本上是指向另一个系统中对象的指针。

所以,添加一个“Brewery”对象。它将从parse中获取objectId,但您应该像在Review类中一样添加“breweryId”字符串属性。它还应该有一个“averageRating”数字属性。

现在我们需要逻辑来计算和保存平均评级,给定一个breweryId(这是外部id,而不是parse objectId)......

// a very helpful toolkit for dealing with js, especially arrays
var _ = require('underscore');

function updateAverageRatingForBreweryWithId(breweryId) {
    var average;
    // get all of the reviews for this breweryId
    var query = new Parse.Query("Review");
    query.equalTo("breweryId", breweryId);
    return query.find().then(function(reviews) {
        // pull out each review's rating attribute
        var ratings = _.map(reviews, function(review) { return review.get("rating"); });
        // this is just an average computation, summing the ratings and dividing by their count
        average = _.reduce(ratings, function(memo, num) { return memo + num; }, 0) / (ratings.length === 0 ? 1 : ratings.length);

        // go get the parse.com Brewery object for this breweryId
        // we will save the average rating there
        var query = new Parse.Query("Brewery");
        query.equalTo("breweryId", breweryId);
        return query.first();
    }).then(function(brewery) {
        brewery.set("averageRating", average);
        return brewery.save();
    });
}

此函数将外部breweryId作为输入,获取该啤酒厂的评论,计算平均值,然后找到(您的新)Brewery对象,并使用该breweryId设置其averageRating并保存它。

在保存评论后,这是一个调用此功能的好地方,这就是您在原始帖子中看起来的样子。必须将afterSave命名为(第一个参数)“Review”,如果你想让它在Review对象上工作(不是你给它的名字或者@RyanKreager因某种原因提到的“Comment”)......

Parse.Cloud.afterSave("Review", function(request) {
    var review = request.object;
    var breweryId = review.get("breweryId");
    updateAverageRatingForBreweryWithId(breweryId);
});

现在有了Brewery对象,我们可以用真正的目标来查询它:我附近评价很高的啤酒厂是什么?我可以为此添加一些额外的建议,但只有在创建Brewery对象时才有意义。

答案 1 :(得分:1)

我看到了一些错误:

  1. 您应该在average对象上存储Brewery字段,而不是评论。这样,您只需更新一个对象,就可以对Brewery等所有average > x个对象进行一次漂亮,整洁的查询。
  2. breweryId是一个字符串。这应该是指向Brewery对象的指针。同时将名称更改为brewery
  3. 一旦你清除了这两件事,这样的事情对afterSave对象上的Review更有意义:
  4. 您的afterSave需要进行一些调整。试试这个:

    Parse.Cloud.afterSave("Comment", function(request) {
       var Review = Parse.Object.extend("Review");
       var query = new Parse.Query(Review);
       query.equalTo("brewery", request.object.get("brewery"));
       query.find({
         success: function(results) {
         // Find the average from all the brewery reviews
         var total = 0;
         for (var i = 0; i < results.length; i++) {
           var object = results[i];
           total += object.get("rating");
         }
         var averageRating = total / results.length;
    
         var updatedObjects = [];
         for (var i = 0; i < results.length; i++) {
           var object = results[i];
           object.set("average", averageRating);
           updatedObjects.push(object);
         }
         Parse.Object.saveAll(updatedObjects);
       }, error: function(error) {
         alert("Error: " + error.code + " " + error.message);
       });
    });