这是我写的解析javascript云代码。我想在子类中找到所有对象"示例"有一个喜欢然后将它们重置为四个。我已经在数据浏览器中建立了Example类和Like列。但是查询没有成功,我无法弄清楚原因。
function exampleFunction() {
var Example = Parse.Object.extend("Example");
var newObject = new Example();
newObject.save(); // until here, the function works, it continues creating new objects
var query = new Parse.Query(Example);
query.equalTo('Like',1);
query.find({
success:function(result){
for (var i = 0; i < result.length; i++) {
result[i].set('Like',4);
}
},
error:function(error){
}
})
}
Parse.Cloud.define("nice", function(request, response) {
exampleFunction();
response.success();
});
我在iOS设备上使用这段代码来触发云功能:
[PFCloud callFunctionInBackground:@"nice"
withParameters:@{}
block:^(NSString *result, NSError *error) {
if (!error) {
}
}];
答案 0 :(得分:1)
有几个可能的问题..
then
函数。请参阅http://blog.parse.com/2013/01/29/whats-so-great-about-javascript-promises/ save
来保存行请尝试以下操作,注意.success
如果您从objective-c NSLog(@"result %@",result)
返回结果,则会返回结果。此外,由于response.error(error)
var Example = Parse.Object.extend("Example");
function exampleFunction() {
var query = new Parse.Query(Example);
query.equalTo('Like',1);
return query.find().then(function(examplesLikedOnce){
var promises = [];
for (var i = 0; i < examplesLikedOnce.length; i++) {
var example = examplesLikedOnce[i];
var promise = example.save({Like:4});
promises.push(promise);
}
return Parse.Promise.when(promises).then(function(){
return examplesLikedOnce.length;
});
});
}
Parse.Cloud.define("nice", function(request, response) {
exampleFunction().then(function(numExamples){
response.success("The number of Example objects with 1 like: "+numExamples);
}, function(error){
response.error(error);
});
});