2

I am using Express in Nodejs in order to make a GET Call. As a response, I get the value that I need, but when I return the value from my function to the main I get undefined.

function getInfo() 
{

      var oauth = getoAuth();

      request.get({
        url:'myurl',   
        oauth:oauth, 
        qs:null, 
        json:true
      }, 
          function (e, r, data) {
            body.data = data;
            body.emit('update');
      }); 


      body.on('update', function () {
        console.log(body.data.issues[0].key);
        return (body.data.issues[0].key);  
      });

}

This key is what I need. When I print it in the console I get the correct value, but it doesn't return anything, because it is an async call. How can I return the value? Can I somehow wait for the value using express? I saw on stackoverflow that some people used this body.on('update'... solution, but it didn't work for me. It still saves nothing to a variable.

EDIT-> the response:

TESTING: undefined
THEKEY1

Saving the value at:

var myid= geInfo();

1 Answer 1

1

try this way :

function getInfo() 
{

      var oauth = getoAuth();

      request.get({ url:'myurl',  oauth:oauth,  qs:null, json:true},
        function (error, response, body) {
            if(!error && response && response.statusCode==200){
               console.log(body);
               return body.data.issues[0].key;  
            }else{
               return null;
            }
      }); 
}
Sign up to request clarification or add additional context in comments.

6 Comments

Dear @Saurabh, again the same :( I also tried to put const theid = body.issues[0].key; return theid; but again I get undefined as return in the main. And the console puts the correct one out. :(
you got undefined , because of that property not exists in body object
@TeslaX , make sure you write correct key body.data.issues[0].key;
I did, because when I print it out it is the correct one. body.data.issues[0].key; ( I am sorry I wrote it in the comment wrong, but in the code it is correct)
it doesn't work. I still get undefined. As on the beginning. It comes out "TESTING: undefined". Couse, it is still async.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.