如何仅在我的回调完成时返回

时间:2014-10-29 15:53:31

标签: javascript node.js asynchronous callback return

我正在编写一个Node.js应用程序,但我无法将web scrape的值返回到我的主app.get函数。页面被抓得很好,结果使它一直到回调中的RETURN,但它实际上并没有返回值。任何帮助将不胜感激!

编辑:这需要是一个纯粹的JavaScript解决方案,不使用jQuery。

在我的server.js文件中,我有这段代码:

var machineDetails = helpers.scrapePage();

app.get('/', function (req, res) {

    res.render('index', { machineDetails: machineDetails, title: 'VIP IT Dashboard'});
});

在helpers.js文件中,我有以下功能

//Requires
httpntlm = require('httpntlm'),
cheerio = require('cheerio');


var userData;

function callSite(callback) {

    //Scrape site and get information
    var scrape;

    httpntlm.get({
        url: "http://URLthatIamScraping.com",
        username: 'username1',
        password: 'password1',
        domain: 'companyDomain'
    }, function (err, res) {
        if (err) return err;

        //Sort information for the computer
        var $ = cheerio.load(res.body);

        var scrape = $('#assetcontent').html();

        //Return the html content of the page
        callback(scrape);

    });
}


exports.scrapePage = function(){

    return callSite(function(data) {

        //This is called after HTTP request finishes
        userData = data;

        //ISSUE: userData is not actually making it back to my server.js variable called "machineDetails"
        return userData;

    });
}

1 个答案:

答案 0 :(得分:1)

它是异步的,你不能只返回值。它必须在回调中返回。

//Requires
httpntlm = require('httpntlm'),
cheerio = require('cheerio');


function callSite(callback) {

    httpntlm.get({
        url: "http://URLthatIamScraping.com",
        username: 'username1',
        password: 'password1',
        domain: 'companyDomain'
    }, function (err, res) {
        if (err) return callback(err);

        //Sort information for the computer
        var $ = cheerio.load(res.body);

        var scrape = $('#assetcontent').html();

        //Return the html content of the page
        callback(null, scrape);

    });
}


exports.scrapePage = callSite;

然后你做:

app.get('/', function (req, res, next) {
    helpers.scrapePage(function(error, machineDetails) {
        if(error) return next(error);
        res.render('index', { machineDetails: machineDetails, title: 'VIP IT Dashboard'});
    });
});