Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.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
Javascript 在请求内部等待请求完成_Javascript_Node.js - Fatal编程技术网

Javascript 在请求内部等待请求完成

Javascript 在请求内部等待请求完成,javascript,node.js,Javascript,Node.js,我有一个代码,从另一个网站请求数据获取链接,在我获取链接后,我将从这些链接获取一些数据。所以,我试着做两个请求 这是我制作的伪代码 [first request to the site] ..fetch links and data ....fetch more data from the fetched links [done] [another function to fetch data from the links fetched] ..fetch data [return fetch

我有一个代码,从另一个网站请求数据获取链接,在我获取链接后,我将从这些链接获取一些数据。所以,我试着做两个请求

这是我制作的伪代码

[first request to the site]
..fetch links and data
....fetch more data from the fetched links
[done]

[another function to fetch data from the links fetched]
..fetch data
[return fetch data]
我试图获得如下输出:

{
    "chimchar" : {
        "articles" : [{
            "id": id,
            "title": title,
            "url": url
        }]
    }
}
但是“url”的值没有显示,我尝试将其登录到控制台,但“url”的值未定义,它在从.getHome函数发出所有请求后显示

这是代码

var cheerio = require('cheerio');
var requestify = require('requestify');

var _response;

/*
getHome
*/
exports.getHome = function(req, res) {
    requestify.get('http://chimchar.com').then(function(response) {
        var __response = processResponse(response);
        res.send(__response);
    });
}

/*
processResponse
*/
function processResponse(response) {
    var id = 0;
    var title, url;

    _response = {
        'chimchar': {
            'articles': []
        }
    };

    response.getBody();
    $ = cheerio.load(response.body);

    $('#scenesContainer .video_box').each(function() {

        $(this).find('img, a').each(function(i, elem) {
            if ($(this).is('img') && $(this).attr('style') == 'margin-top: 35px;')
                title = $(this).attr('alt');
            if ($(this).is('a') && $(this).attr('class') == 'link_block') {
                fetchPage($(this).attr('href'), function(returnValue) {
                    url = returnValue;
                    console.log(url);
                });
            }
        });

        _response.chimchar.articles.push({
            "id": id,
            "title": title,
            "url": url,
        });

        id = id + 1;
    });

    return _response;
}

var fetchPage = function(page, callback) {
    console.log('fetchPage');
    requestify.get('http://chimchar.com' + page).then(function(response) {
        var r;
        response.getBody();
        $ = cheerio.load(response.body);

        $('.play_video').each(function() {
            $(this).find('a').each(function(i, elem) {
                if ($(this).is('a') && $(this).text().indexOf('MP4') > -1) {
                    r = $(this).attr('href');
                    console.log(r);
                }
            });
        });

        console('fetchPage finished - ' + r);
        callback(r);
    });
};
我想

return _response;
在processResponse函数中获取url之前激发

我认为您需要使用异步,代码如下所示

function processResponse(response, callback) {
    async.each([items], function (item, cb) {
        doSomething(item, function () {
            //Make all operations with response
            cb(null, true)
        });
    },
    function (err, result) {
       callback(_response)
    })
}

谢谢你的建议,我会试着告诉你发生了什么。再次感谢!