title: Parse cloud code: simple function not working
body: <p>I'm trying to implement a <code>beforeSave</code> function on cloud code and the following simple function doesn't work when there are multiple hashtags. Given if <code>Product</code> has two hashtags and the <code>Hashtag</code> table is empty, the following parse cloud code stores the second hashtag twice with a <code>count</code> of 1:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>Parse.Cloud.beforeSave("Product", function(req, res) {
    var product = req.object;
    var hashtags = product.get("hashtags");
    for (var i = 0; i &lt; 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");
        }
      });
    }
  });</code></pre>
  </div>
  </div>
  </p>