[英]Parse cloud code: simple function not working
I'm trying to implement a beforeSave
function on cloud code and the following simple function doesn't work when there are multiple hashtags. 我正在尝试在云代码上实现
beforeSave
函数,并且当存在多个主题标签时,以下简单函数不起作用。 Given if Product
has two hashtags and the Hashtag
table is empty, the following parse cloud code stores the second hashtag twice with a count
of 1: 给定如果
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"); } }); } });
change this line var hashtagObj = results[0]; 更改此行var hashtagObj = results [0]; to this line
到这条线
var hashtagObj = results[i];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.