简体   繁体   English

并行API请求的异步模块错误

[英]Async module error with parallel API requests

I'm a begginer in node.js and now I'm trying to get some API's result. 我是node.js的初学者,现在我正试图获得一些API的结果。 I'm using async module ( https://github.com/caolan/async ) to parallel the request so it can be optimized. 我正在使用异步模块( https://github.com/caolan/async )来并行请求,以便对其进行优化。

The problem is the code is returning an error that each time points to a different line (the "callback(err, data)" line) in a different API call 问题是代码返回一个错误,每次指向不同API调用中的不同行(“回调(错误,数据)”行)

That's is the error: 那就是错误:

if (called) throw new Error("Callback was already called.");
                          ^
Error: Callback was already called.

I'm using the following function to request the API: 我正在使用以下函数来请求API:

function getData(s, apiURL, getURL, callback) {

    var ht;
    if (s == 1) {
        ht = https;
    } else {
        ht = http;
    }

    var options = {
        hostname : apiURL,
        path : getURL,
        method : 'GET'
    }

    var content = "";

    ht.get(options, function(res) {
        console.log("Got response: " + res.statusCode);

        res.on("data", function(chunk) {

            content += chunk;
            //console.log(content);
            callback(content);
       });

    }).on('error', function(e) {
        console.log("Error: " + e.message);
        console.log(e.stack);
    });
}

To illustrate, this is how I'm using the async module: 为了说明,这就是我使用异步模块的方式:

var sources = sources.split(',')

    async.parallel({
        flickr : function(callback) {
            if (sources[0] == 1) {
                getData(0, 'api.flickr.com',
                "/services/rest/?method=flickr.photos.search&format=json&nojsoncallback=1&api_key=" + config.flickr.appid + "&per_page=5&tags=" + tags,

                function(data) {
                    callback(null, data); //here that the error may point
                });

            } else { callback(); }
        },
        instagram : function(callback) {
            if (sources[1] == 1) {

                getData(1, 'api.instagram.com',
                "/v1/tags/" + tags.split(',')[0] + "/media/recent?client_id=" + config.instagram,
                function(data) {                        
                    callback(null, data); //here that the error may point

                });
            } else { callback(); }
        } 
    }, function(err, results) {             
            console.log(results);
        });

Hope you can help me, thanks! 希望你能帮助我,谢谢!

You are calling the callback function inside the data event ( see the docs ), which is triggered every time a chunk of data is received, that means several times. 您正在调用data事件中的回调函数( 请参阅文档 ),每次收到一大块数据时都会触发该函数,这意味着多次。

Async.parallel is expecting the callback to be executed only once for each task. Async.parallel期望每个任务只执行一次回调。

To ensure only one callback per task, put the callback on an end event, for example. 例如,要确保每个任务只有一个回调,请将回调放在end事件上。

Also, the error event should execute the callback as well. 此外,错误事件也应该执行回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM