Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.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_Asynchronous_Callback_Return - Fatal编程技术网

Javascript 如何仅在回调完成时返回

Javascript 如何仅在回调完成时返回,javascript,node.js,asynchronous,callback,return,Javascript,Node.js,Asynchronous,Callback,Return,我正在编写一个Node.js应用程序,但在将web scrape的值返回到主app.get函数时遇到问题。页面被刮得很好,结果一直到我的回调中的返回,但实际上它并没有返回值。任何帮助都将不胜感激 编辑:这需要是一个纯javascript解决方案,并且不使用jQuery 在我的server.js文件中,我有以下代码: var machineDetails = helpers.scrapePage(); app.get('/', function (req, res) { res.ren

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

编辑:这需要是一个纯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;

    });
}

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

//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'});
    });
});

杰出的我将
helper.scrapePage
移动到app.get之外,这样就不会每次有人访问
'/'
时都调用它。非常感谢!