Can`t use the response from a get request using callback function

Mirel Ionut Puchianu

I make a get request from a url, and I try to use the response further, in another function, so this is what I tried first.

var request = require("request");

function getJSON(getAddress) {
    request.get({
        url: getAddress,
        json: true,
    }, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            return body;
        }
     })
 }

function showJSON(getAddress, callback) {
    var test = callback(getAddress);
    console.dir(test);
}

showJSON('http://api.open-notify.org/astros.json', getJSON);

yet, when i run my script

node ./test.js 

I get

'undefined' as a console message

I don`t know where this may come from, as I am new to node.js, javascript

marvel308
var test = callback(getAddress);

is an asynchronous function

console.dir(test);

won't wait for it to finish before executing, hence you are getting undefined. to make it work you would have to do

var request = require("request");

function getJSON(getAddress, callback) {
    request.get({
        url: getAddress,
        json: true,
    }, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            callback(body);
        }
     })
 }

function showJSON(getAddress, callback) {
    callback(getAddress, function(test){
        console.dir(test);
    });
}

showJSON('http://api.open-notify.org/astros.json', getJSON);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't get data from other html using $.get function(response)

From Dev

Can't get response from returned Request in Scrapy

From Dev

Unable to use callback to get response from ajax call

From Dev

Using ExtAudioFileWriteAsync() in callback function. Can't get to run

From Dev

Can't get Superfeedr callback response in CakePHP

From Dev

Android how to get response string from Callback using OkHttp?

From Dev

How to get response text from ajax callback in a variable using ExtJS

From Dev

How can I get the second response from service with callback method?

From Dev

How use GET request with web API for JSON response using HttpURLConnection?

From Dev

How to get and use a JSON parameter value from an API request/response

From Dev

Use "this" pointer from inside map callback function using ReactJs

From Dev

How to get data from JSON response using Alamofire request

From Dev

How to get the response text from an AJAX request using jQuery

From Dev

Cannot get response from PUT request using Slim Framework

From Dev

Trying to return a row from MySql using a function inside a route and won't get a response

From Dev

Can not use the `this` in my callback function

From Dev

Cannot get response in JSONP proxy callback when request failed

From Dev

get response from GET request to API

From Dev

Get Response URL from GET Request

From Dev

Sending response to client from within the request.post callback in koa

From Dev

Can't get data from table using function belongsToMany

From Dev

Can't get data from table using function belongsToMany

From Dev

Can't get item from a JSON response

From Dev

what callback function should i use in an XMLHttpRequest to render the response?

From Dev

using data from GET request in react and use in child component

From Dev

swift http request can't get non-200 response

From Dev

can't print response data after sending guzzle `get` request

From Dev

can't print response data after sending guzzle `get` request

From Dev

swift http request can't get non-200 response

Related Related

  1. 1

    Can't get data from other html using $.get function(response)

  2. 2

    Can't get response from returned Request in Scrapy

  3. 3

    Unable to use callback to get response from ajax call

  4. 4

    Using ExtAudioFileWriteAsync() in callback function. Can't get to run

  5. 5

    Can't get Superfeedr callback response in CakePHP

  6. 6

    Android how to get response string from Callback using OkHttp?

  7. 7

    How to get response text from ajax callback in a variable using ExtJS

  8. 8

    How can I get the second response from service with callback method?

  9. 9

    How use GET request with web API for JSON response using HttpURLConnection?

  10. 10

    How to get and use a JSON parameter value from an API request/response

  11. 11

    Use "this" pointer from inside map callback function using ReactJs

  12. 12

    How to get data from JSON response using Alamofire request

  13. 13

    How to get the response text from an AJAX request using jQuery

  14. 14

    Cannot get response from PUT request using Slim Framework

  15. 15

    Trying to return a row from MySql using a function inside a route and won't get a response

  16. 16

    Can not use the `this` in my callback function

  17. 17

    Cannot get response in JSONP proxy callback when request failed

  18. 18

    get response from GET request to API

  19. 19

    Get Response URL from GET Request

  20. 20

    Sending response to client from within the request.post callback in koa

  21. 21

    Can't get data from table using function belongsToMany

  22. 22

    Can't get data from table using function belongsToMany

  23. 23

    Can't get item from a JSON response

  24. 24

    what callback function should i use in an XMLHttpRequest to render the response?

  25. 25

    using data from GET request in react and use in child component

  26. 26

    swift http request can't get non-200 response

  27. 27

    can't print response data after sending guzzle `get` request

  28. 28

    can't print response data after sending guzzle `get` request

  29. 29

    swift http request can't get non-200 response

HotTag

Archive