-1

I would like to return some of the body from a request made in request.js to the module that calls it in app.js

I have tried using module.exports to save information in the body. I know that the request is asynchronous so I have to wait for it to come back but I am not sure how to do this.

My app.js file:

const yargs = require('yargs');
const fs = require('fs');


const argv = yargs.
options({
    capcity: {
        description: 'capital city',
        alias: 'c',
        demand: true
    }
})
.help()
.argv;

module.exports.capcity = argv.capcity;

const requestData = require('./requestData.js');

console.log(requestData.country);

My requestData.js file:

const request = require('request');
const app = require('./app.js');

var country;

request({
    url: `https://restcountries.eu/rest/v2/capital/${app.capcity}`,
    json: true
}
, (error, response, body) => {

    console.log(app.capcity);
    if (error) {
        console.log('error:', error);
    } else if (body.status == 404) {
        console.log(JSON.stringify(body, undefined, 2));
        console.log("invalid city entered");
    } else {
        country = {
            name: body[0].name,
            code: body[0].currencies[0].code,
            symbol: body[0].currencies[0].symbol
        }
        country = JSON.stringify(country);
        console.log(country);
    }
});

module.exports.country = country;

It returns that country is undefined.

dran
  • 9
  • 1
  • Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – CertainPerformance Jun 14 '19 at 10:04

1 Answers1

0

You've got some race conditions going on here, You many want to some research on callbacks, or better yet switch to using promises. Also you may want to export functions instead of variables, your code will be more reusable that way.

Heres an example using promises. note I'm using request-promise-native as suggested in the request documentation. you would need to install that dependency also to use this.

In your app.js

const yargs = require('yargs');
const fs = require('fs');


const argv = yargs.
options({
    capcity: {
        description: 'capital city',
        alias: 'c',
        demand: true
    }
})
.help()
.argv;

const capcity = argv.capcity;

const requestDataFile = require('./requestData.js');

requestDataFile.requestData(capcity).then(country=>{
    console.log(country)
})

In your requestData file

const request = require('request-promise-native');
const app = require('./app.js');


module.exports.requestData = (capcity)=>request({
        url: `https://restcountries.eu/rest/v2/capital/${capcity}`,
        json: true
    }).then((body)=>{
        return country = {
            name: body[0].name,
            code: body[0].currencies[0].code,
            symbol: body[0].currencies[0].symbol
        }
    })
  • Thank you, I ended up wrapping the request method in its own function then exporting that so I could call it from app.js and it seemed to work, and just wrote to file from the request module. – dran Jun 17 '19 at 10:05