Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 请求npm模块-请求内部的请求_Node.js_Npm_Npm Request - Fatal编程技术网

Node.js 请求npm模块-请求内部的请求

Node.js 请求npm模块-请求内部的请求,node.js,npm,npm-request,Node.js,Npm,Npm Request,我正在使用请求npm模块,并在主流请求中提出4个请求。首先说结论,四个请求中只有两个随机成功 下面是我的代码 router.get('/', function(req, res){ //TODO request(url, function(error, response, body) { if(err) throw error; //TODO- request(comnURL, function(errp,resp, body){

我正在使用请求npm模块,并在主流请求中提出4个请求。首先说结论,四个请求中只有两个随机成功

下面是我的代码

router.get('/', function(req, res){
    //TODO
    request(url, function(error, response, body) {
        if(err) throw error;
        //TODO-
        request(comnURL, function(errp,resp, body){
            if(errp) throw errp;
            comnBODY = body;
            console.log(body);
            console.log("\n\n");
        });
        request(intrURL, function(errp,resp, body){
            if(errp) throw errp;
            intrBODY = body;
            console.log(body);
            console.log("\n\n");
        });
        request(reptURL, function(errp,resp, body){
            if(errp) throw errp;
            reptBODY = body;
            console.log(body);
            console.log("\n\n");
        });
        request(addiURL, function(errp,resp, body){
            if(errp) throw errp;
            addiBODY = body;
            console.log(body);
            console.log("\n\n");
        });
        //TODO-
    });
});

每个响应请求随机不同,从4个子请求中选择2个子请求。这可能是什么原因,以及如何避免它。

您的代码有一些语法错误,但工作正常。这可能是一个异步问题。如果希望按顺序执行此4请求,则必须按以下方式执行:

'use strict';

const request = require('request')

request('https://jsonplaceholder.typicode.com/posts/5', function(error, response, body) {
    if(error) throw error;
    //TODO-
    request('https://jsonplaceholder.typicode.com/posts/1', function(errp,resp, body){
        if(errp) throw errp;
        let comnBODY = body;
        console.log(body);
        console.log("1\n\n");
        request('https://jsonplaceholder.typicode.com/posts/2', function(errp,resp, body){
            if(errp) throw errp;
            let intrBODY = body;
            console.log(body);
            console.log("2\n\n");
            request('https://jsonplaceholder.typicode.com/posts/3', function(errp,resp, body){
                if(errp) throw errp;
                let reptBODY = body;        
                console.log(body);
                console.log("3\n\n");
                request('https://jsonplaceholder.typicode.com/posts/4', function(errp,resp, body){
                    if(errp) throw errp;
                    let addiBODY = body;
                    console.log(body);
                    console.log("4\n\n");
                });
            });
        });
    });
});
另一种方法是使用承诺方式,因为您可以使用