javascript NodeJS 返回 http.request

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9332856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:18:14  来源:igfitidea点击:

NodeJS return http.request

javascriptnode.js

提问by Nick White

Hi now I know that NodeJS is asynchronous (which I am still trying to get my head around to be honest).

嗨,现在我知道 NodeJS 是异步的(说实话,我仍在努力解决这个问题)。

The problem that I am currently facing is I have attempting to do a http.requestto receive some JSON data. This is fine but what I need is to return this data to a variable. I believe I need to do a callback function? (from what I have read up on the matter)

我目前面临的问题是我试图http.request接收一些 JSON 数据。这很好,但我需要的是将此数据返回给变量。我相信我需要做一个回调函数吗?(根据我对此事的了解)

The Code that I currently have:

我目前拥有的代码:

var http = require('http');
pCLatLng = '';

function postCodeCheck() {
    var pCode = { 
        host: 'geo.jamiethompson.co.uk',
        path: "/" + 'SW1A2AA' + ".json" 
    };

    http.request(pCode).on('response', function(response) {
        var data = '';
        response.on("data", function (chunk) {
            data += chunk;
        });
        response.on('end', function () {
            pCJSON = JSON.parse(data);
            pCLatLng = pCJSON;
        });
        }).end();
}

console.log(pCLatLng);

This is obviously outputting "undefined"; I have tried returning the response.on('end') when having return "hi" or anything inside it instead NodeJS outputs the information about the site. If anyone can help with this it would be much appreciated.

这显然是在输出“undefined”;我尝试在返回“hi”或其中的任何内容时返回 response.on('end') 而不是 NodeJS 输出有关站点的信息。如果有人可以帮助解决这个问题,我们将不胜感激。

回答by James McLaughlin

console.log(pCLatLng);needs to be inside (or inside something called by) the response.on('end'callback. The value isn't available until that callback is fired.

console.log(pCLatLng);需要在response.on('end'回调内部(或被调用的东西内部)。在触发该回调之前,该值不可用。

Try something like:

尝试类似:

function postCodeCheck(callback) {
    var pCode = { 
        host: 'geo.jamiethompson.co.uk',
        path: "/" + 'SW1A2AA' + ".json" 
    };

    http.request(pCode).on('response', function(response) {
        var data = '';
        response.on("data", function (chunk) {
            data += chunk;
        });
        response.on('end', function () {
            callback(JSON.parse(data));
        });
        }).end();
}

postCodeCheck(function (pCLatLng)
{
    console.log(pCLatLng);
});

回答by Dan D.

You want something like this:

你想要这样的东西:

var http = require('http');

function postCodeCheck(cb) {
    var pCode = { 
        host: 'geo.jamiethompson.co.uk',
        path: "/" + 'SW1A2AA' + ".json" 
    };

    http.request(pCode).on('response', function(response) {
        var data = '';
        response.on("data", function (chunk) {
            data += chunk;
        });
        response.on('end', function () {
            var pCJSON = JSON.parse(data);
            cb(pCJSON);
        });
        }).end();
}

postCodeCheck(function(pCLatLng) { console.log(pCLatLng); });

Look carefully for the differences before using.

使用前请仔细查看差异。

回答by tmcw

You'll need your postCodeCheck()function to take a callback as well, just like http.request. In the world of async, calling callbacks with results takes a similar role to returning results.

您还需要您的postCodeCheck()函数进行回调,就像http.request. 在异步世界中,调用带有结果的回调与调用结果具有类似的作用return