我尝试在云代码上实现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;
答案 0 :(得分:0)
更改此行var hashtagObj = results [0];到这一行
var hashtagObj = results[i];