解析云代码:简单的功能无法正常工作

时间:2016-06-26 16:01:31

标签: javascript parse-platform cloud

我尝试在云代码上实现beforeSave功能,并且当存在多个主题标签时,以下简单功能不起作用。假设Product有两个主题标签并且Hashtag表为空,则以下解析云代码将第二个主题标签存储两次,其中count为1:



Parse.Cloud.beforeSave("Product", function(req, res) {
  var product = req.object;
  var hashtags = product.get("hashtags");
  for (var i = 0; i < hashtags.length; i++) {
    var currentHashtag = hashtags[i].toLowerCase();
    var hashtagQuery = new Parse.Query("Hashtag")
    hashtagQuery.equalTo("label", currentHashtag);
    hashtagQuery.find({
      success: function(results) {
        if (results.length == 0) {
          var hashtagClass = Parse.Object.extend("Hashtag");
          var newHashtag = new hashtagClass();
          newHashtag.set("label", currentHashtag);
          newHashtag.set("count", 1);
          newHashtag.save();
        } else {
          var hashtagObj = results[0];
          hashtagObj.increment("count");
          hashtagObj.save();
        }
        if (i == hashtags.length - 1)
          res.sucesss();
      },
      error: function() {
        res.error("Oops");
      }
    });
  }
});
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

更改此行var hashtagObj = results [0];到这一行

var hashtagObj = results[i];