Parse.Cloud.httpRequest 未返回响应

Parse.Cloud.httpRequest not returning response

我正在尝试进行 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.");
 });
});

Parse.Cloud.httpRequest() 是一个异步函数调用。它不会阻塞线程,因此在从 httpRequest() 获得结果之前,您的代码会保留 运行 status.success("Saved successfully.");

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

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.");
    });
});


编辑

each()不一样,请直接在httpRequest中添加成功回调。

Parse.Cloud.job("Loader", function(request, status) {
    var allHttpResponseTexts = '';

    var query = new Parse.Query("Codes");
    query.each(function(a) {
        return Parse.Cloud.httpRequest({
            url: 'http://example.com',
            success: function(httpResponse) {
                // Process httpResponse.text here

                allHttpResponseTexts += httpResponse.text.substr(0, 50);
            }
        });
    }).then(function(httpResponse) {
        status.success("Saved successfully. allHttpResponseTexts: " + allHttpResponseTexts);
    }, function(error) {
        status.error("Uh oh, something went wrong. " + error.code + ' - ' + error.message);
    });
});

最后的结果是:

Saved successfully. allHttpResponseTexts: <!doctype html> <html> <head> <title>Example D...