我需要在Parse.Cloud.Job中实现以下内容:
目前我可以从我的班级中检索所有对象(步骤1)但是当我尝试进行httpRequest(步骤2)时,云作业已经完成。
Parse.Cloud.job("myCloudJob", function(request, status){
var countObjects;
var objectsArray = [];
var query = new Parse.Query("MyClass");
query.limit(200);
query.find().then(function(results){
countObjects = results.length;
objectsArray = results;
}).then(function(){
for(i = 0; i < countObjects; i++){
var valueOne = objectsArray[i].attributes.valueOne;
Parse.Cloud.httpRequest({
url: 'https://www.myApi.com/',
params: {value: valueOne}
}).then(function(httpResponse) {
console.log(httpResponse.status);
response.success('Status: ' + httpResponse.status + 'Response: ' + httpResponse.text);
}, function(httpResponse) {
console.error('Request failed with response code ' + httpResponse.status);
response.error('Error, status ' + httpResponse.status + ' data' + httpResponse.text);
});
}
status.success("There " + countObjects + " objects in MyClass.");
}, function(error) {
status.error("Error: " + error.code + " " + error.message);
}
);
});
&#13;
有人可以帮我完成这项任务吗?
更新:
Parse.Cloud.job("myCloudJob", function(request, status){
var countObjects;
var objectsArray = [];
var query = new Parse.Query("MyClass");
query.limit(200);
query.find().then(function(results){
countObjects = results.length;
console.log("There " + countObjects + " objects to process.");
objectsArray = results;
}).then(function(){
for(var i = 0; i < countObjects; i++){
var promises = [];
console.log("i: " + i + " promises " + promises);
var objectId = objectsArray[i].id;
var myValue = objectsArray[i].attributes.value;
promises.push(Parse.Cloud.httpRequest({
url: 'https://www.myApi.com',
params: {value: myValue}
})
.then(function(httpResponse){
var dataFromResponse = httpResponse.data;
var Point = Parse.Object.extend("MyClass");
var point = new Point();
point.id = objectId;
point.set("value", dataFromResponse);
return point.save();
}));
return Parse.Promise.when(promises);
}
}).then(function(){
status.success("Job completed");
},function(){
status.error("Error running Job");
});
});
&#13;
答案 0 :(得分:1)
在http请求完成之前,您正在调用status.success。代码需要等到它们全部完成。您将这些承诺保存在数组中并在调用status.success之前等待。 https://www.parse.com/docs/js/guide#promises-promises-in-parallel
尝试以下代码
Parse.Cloud.job("myCloudJob", function(request, status){
var countObjects;
var objectsArray = [];
var query = new Parse.Query("MyClass");
query.limit(200);
query.find().then(function(results){
countObjects = results.length;
objectsArray = results;
}).then(function(){
for(var i = 0; i < countObjects; i++){
var promises = [];
promises.push(Parse.Cloud.httpRequest({
url: 'https://www.myApi.com/',
params: {value: valueOne}
})
.then(function(httpResponse){
//process your reponse here
//for example if you want to insert some data to parse
//here is how you would do it.
var myObject2 = Parse.Object.extend("Object2");
myObject2.set("Prop1",httpResponse.data.propertyName);
return myObject2.save();
}));
return Parse.Promise.when(promises);
}
}).then(function(){
status.success("Job completed");
},function(){
status.error("Error running Job");
});
});