通过 parse.cloud.httpReq 发送多个 http 请求 [英] Send multiple http request via parse.cloud.httpReq

查看:49
本文介绍了通过 parse.cloud.httpReq 发送多个 http 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Parse.Cloud.httpRequest 发送多个 HTTP 请求,但我收到

I'm trying to send multiple HTTP Requests via Parse.Cloud.httpRequest but I get

{"code":141,"message":"错误:无法多次调用成功/错误

{"code":141,"message":"Error: Can't call success/error multiple times

我需要处理所有请求并将结果放入响应"并返回响应,以便我可以在另一个函数中使用它.

I need to do all requests and put results in " responses " and return responses back so I can use it in another function.

或者,如果有人可以指导我,我可能走错了方向.

OR maybe I'm in the wrong direction if anyone could guide me.

问候

Parse.Cloud.define('http', function(request, response) {

var query = new Parse.Query(Parse.Installation);
var responses = new Array ();

for (var i = 0; i < request.params['params'].length; i++) {
      var object = request.params['params'][i];

      Parse.Cloud.httpRequest({
          url: 'http://185.xxxxxxx'+ object +'&languagePath=en',
          success: function(httpResponse) {
            responses.push(httpResponse);
          }
        }).then(function(httpResponse) {
          console.log('Request Succeeded with response Data ' + httpResponse.text);
          response.success(responses);
        },function(error) {
          // error
          console.error('Request failed with response code ' + httpResponse.status);
        }); 
    }
});

推荐答案

诀窍是收集 promise 以完成 http 请求,然后在 promise 完成时设置响应.

The trick is to collect promises to complete the http requests, then set the response "when" the promises are complete.

Parse.Cloud.define('http', function(request, response) {
    var promises = [];

    for (var i = 0; i < request.params['params'].length; i++) {
        var object = request.params['params'][i];
        promises.push(makeRequestWithObject(object));
    }
    Parse.Promise.when(promises).then(function() {
        // results are passed to this function as var args
        response.success(arguments);
    }, function(error) {
        response.error(error);
    });
});

// return a promise to complete an http request with object
function makeRequestWithObject(object) {
    var url = 'http://185.xxxxxxx'+ object +'&languagePath=en';
    return Parse.Cloud.httpRequest({ url:url });
}

这篇关于通过 parse.cloud.httpReq 发送多个 http 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆