0

I have a REST end point and trying to hit by using 'request' library in node.js by using the below file:

I just updated this question with latest observations.

Added new function called delayFunction() as below:

Util.js

  request = require('request')

  var delay1 = {
     delayFunction: function() {
        return new Promise(function(resolve, reject) {
        setTimeout(function() {
        resolve(42); // After 3 seconds, resolve the promise with value 42
        }, 3000);
      });
    }
  }

    var getJSONValue1 = {

        getJSON: function(){

             let link=[];

             let file ="http://localhost:8080/idsearch/environment/amazon/cartId/2342cd1210a5";

             var headers ={
               "accept":"application/json"
             }

            var options = {
                url: file,
                headers: headers
            }

            request(options, function(error,response, body){

                 if(response.statusCode==200)
                   {
                    //return response.body {This is also not working} 
                     console.log('yes');
                     link.push(response.body);
                     await delay1.delayFunction();
                   }
            });

            console.log(link);

        }
    }

  module.exports = {

    getFunction: function(){
        return getJSONValue1.getJSON();
    }

 }

The above payload I'm trying to return the response body and printing into test.js file as below:

test.js

  var util = require(__dirname + '/util');

   let value=util.getFunction();

   console.log(value);

But I'm unable to get the response and It says undefined as below output.

Output:

[] 
undefined 
yes

Updated Code:

Observed error:

              await delay1.delayFunction();
                ^^^^^

              SyntaxError: await is only valid in async function

Can anyone help me to get the response body which is only fly and store it into variable and use it for further reference.

ArrchanaMohan
  • 2,314
  • 4
  • 36
  • 84
  • Can you debug the code using console.log() right after request(options, function(error,response, body){ console.log(response); console.log(error); console.log(body); ....... } and please comment out await while debugging as it cause the error – Ajith Nov 27 '19 at 07:54
  • I got the Payload when i use console.log(response.body); but its bit huge and taking time to print. The problem is unable to return it – ArrchanaMohan Nov 27 '19 at 08:00
  • What happen when return response.body and comment out below codes, inside that block? – Ajith Nov 27 '19 at 08:18
  • Can you explain your requirement in the question, from the code I understood you are trying to take data from a url on each time interval of 3 seconds. If you explain your requirement some one can give another alternate solution – Ajith Nov 27 '19 at 08:22
  • I just want to return the response which is json format and stored into array or variable. I just want to loop that json to get value of some node . but i stuck to return the response itself. If i tried to write in fs it works fine but its taking time to write and this leads error to iterate the nodes in json. If i keep these two separate it works fine. But how can i achieve both all together . – ArrchanaMohan Nov 27 '19 at 08:30
  • 1
    In any case you should write "request(options, async function(error,response, body){..." to be able to use await. –  Nov 27 '19 at 12:23
  • It worked thanks vicraj...!!! Please post your answer I will accept it. – ArrchanaMohan Nov 28 '19 at 04:47

0 Answers0