我在Cloud Code中遇到HTTP发布请求时出现问题,并且出于某种原因,它实际上没有找到成功或错误消息。它将注销我已编码的JSON变量,但似乎根本不执行HTTP请求。关于我的问题是什么想法?
Parse.Cloud.job("getUsers", function(request, status) {
// Query to get users
var users = Parse.Object.extend("_User");
var query = new Parse.Query(users);
var t = 0;
query.notEqualTo("mkUsername", "");
query.notEqualTo("mkPassword", "");
query.equalTo("canUseMK", "1");
query.find({
success: function(results) {
for (var i = 0; i < results.length; ++i) {
var object = results[i];
var requestVariables = { mkUsername: object.get("mkUsername"), mkPassword:object.get("mkPassword"), userID: object.id };
var requestJsonString = JSON.stringify(requestVariables);
console.log(requestJsonString);
Parse.Cloud.httpRequest({
method: 'POST',
url: 'http://url.com/api_trial.php',
headers: {
'Content-Type': 'application/json;charset=utf-8',
'authKey' : 'XXXXXXXXXX'
},
body:requestJsonString,
success: function(httpResponse) {
++t;
console.log(httpResponse.text);
response.success(httpResponse.text);
status.message("User "+ i + " Response " + httpResponse.status);
},
error: function(httpResponse) {
//get the status code
if (httpResponse.status == 400) {
console.error('Request failed with response code ' + httpResponse.status);
response.error('Request failed with response code ' + httpResponse.status);
}
else if (httpResponse.status == 401) {
console.error('Request failed with response code ' + httpResponse.status);
response.error('Request failed with response code ' + httpResponse.status);
}
else if (httpResponse.status == 409) {
console.error('Request failed with response code ' + httpResponse.status);
response.error('Request failed with response code ' + httpResponse.status);
} else {
console.error('Request failed with response code ' + httpResponse.status);
response.error('Request failed with response code ' + httpResponse.status);
}
}
});
}
status.success("Synced "+ i + " Users Data");
},
error: function(error) {
status.error("Had query issue: " + error);
}
});
});
答案 0 :(得分:0)
HTTP请求以异步方式执行,但在启动可能尚未完成的HTTP请求后直接调用status.success
。
这将使作业返回就像完成一样,并且应该终止任何正在运行的HTTP请求,从而导致您看到的行为。将status.success
的调用移动到您的请求的成功函数中,它应该没问题。