Parse.Cloud.httpRequest没有返回响应

时间:2015-06-18 01:53:33

标签: parsing parse-platform cloud-code

我正在尝试进行Parse.Cloud.httpRequest调用,作业执行成功,但我没有得到任何响应。如果我在RestClient上运行请求它执行正常但由于某种原因它在Parse Cloud Code中不起作用。我做错了什么?

Parse.Cloud.job("Loader", function(request, status) {

var xmlreader = require('cloud/xmlreader.js');
var moment  = require('cloud/moment.js');

var query = new Parse.Query("Codes");
query.each(function(a) {
  var curDateMonth = moment().date();
  var curMonth = moment().add(1, 'months').month();
  var curYear = moment().year();

  Parse.Cloud.httpRequest({
  url: 'https://.....'
}).then(function(httpResponse) {
  console.log(httpResponse.text); 
}, function(httpResponse) {
  console.error('Request failed with response code ' + httpResponse.status);
});

}).then(function() {
    // Set the job's success status
status.success("Saved successfully.");
}, function(error) {
// Set the job's error status
status.error("Uh oh, something went wrong.");
 });
});

1 个答案:

答案 0 :(得分:1)

Parse.Cloud.httpRequest()是一个异步函数调用。它不会阻塞线程,所以在从httpRequest()获得结果之前,代码会一直运行到function alertText($text) { echo " <script> alert($text); </script> "; } alertText("Hello"); ?>

Parse.Cloud.httpRequest()现在返回一个Promise,所以你可以简单地将它们链接在一起。

status.success("Saved successfully.");


修改

Parse.Cloud.job("Loader", function(request, status) { var xmlreader = require('cloud/xmlreader.js'); var moment = require('cloud/moment.js'); var query = new Parse.Query("Codes"); query.each(function(a) { var curDateMonth = moment().date(); var curMonth = moment().add(1, 'months').month(); var curYear = moment().year(); return Parse.Cloud.httpRequest({ url: 'https://.....' }); }).then(function(httpResponse) { console.log(httpResponse.text); status.success("Saved successfully. httpResponse: " + httpResponse.text); }, function(error) { // Set the job's error status status.error("Uh oh, something went wrong."); }); }); 有所不同,所以请直接将成功回调添加到httpRequest。

each()

最终结果是:

  

成功保存。 allHttpResponseTexts:&lt;!doctype html&gt; &LT; HTML&GT;   &LT; HEAD&GT; &lt; title&gt;示例D ...