2
app.post('/getDetails', async function (req, res) {
    var body
    var post_req = await https.request(post_options, function (res) {
        res.setEncoding('utf8');
        body = '';
        res.on('data', function (chunk) {
            body += chunk;
            console.log(body);
            console.log(JSON.parse(body));
            newBody = body;
        }).on('error', function (error) {
            console.log(error)
        });
    });
    post_req.write(requestBody);
    post_req.end();
    res.send(body);

});

res.send(body) is sending undefined I have put a console.log(body) before res.send(body) it is showing undefined and inside variable post_req the body is having value

Ali Elkhateeb
  • 3,413
  • 4
  • 21
  • 39
krypton
  • 61
  • 2
  • 7
  • duplicate of https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – AZ_ May 15 '19 at 06:38
  • couldn't get a clear picture I have used async/await in my function – krypton May 15 '19 at 06:42
  • async await is for promises but you're initiating body variable inside a callback. – AZ_ May 15 '19 at 06:45
  • Putting a seTimeout over the res.send(body) is a good practice as my body will be initialized to the value coming from callback function or shall I do something else – krypton May 15 '19 at 06:51
  • why not use Fetch or request-promise to call an API? or wrap your callback inside a promise. – AZ_ May 15 '19 at 06:54
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – 1565986223 May 15 '19 at 06:57
  • You are trying to combine async-await with callback. Duplicate of https://stackoverflow.com/questions/55687275/undefined-value-after-returning-an-array-of-values-from-a-mysql-query-in-a-diffe/55688488#55688488 – Sreehari May 15 '19 at 07:09
  • you have to call res.send(body); in side from request , or you have wait till for request id process . It is coming undefined as you request is not serve by server and function in process ahead as you are not waiting for response – Amit Rai May 15 '19 at 07:19

1 Answers1

0

As I mentionedin the comment, you are trying to use Async-Await with callback. You cannot use the Async-Await syntax with callback. Use Async/Await with promises.

You have to write promise wrapper for your callback or you can use request-promise module to return promise.

Promise-wrapper:

app.post('/getDetails', async function (req, res) {
    var body

    function httpReq(post_options){
      return new Promise(function(resolve, reject){
         https.request(post_options, function (res) {
            res.setEncoding('utf8');
            body = '';
            res.on('data', function (chunk) {
               body += chunk;
               console.log(body);
               console.log(JSON.parse(body));
               newBody = body;
               resolve();
            }).on('error', function (error) {
               console.log(error)
               reject()
            });
         });
      })
    }

    var post_req = await httpReq(post_options)
    ...
    ...
    res.send(body);

});
Sreehari
  • 1,328
  • 11
  • 29