Home > Net >  Export value after axios call response using Node.js
Export value after axios call response using Node.js

Time:10-12

I need one solution. I am using axios module to fetch response from other server and after that I need to export value to other file using Node.js. I am explaining my code below.

const axios = require('axios');
let protocol = '';
let opticalIp = '';


axios.get(`https://10.0.12.17:9201/api/v1.0/settings/custom-form/Optical`).then(function(response) {
    //console.log('succ', response);
    if(response.data && response.data.data) {
        protocol = response.data.data['scheme'];
        opticalIp = response.data.data['optical_ip'];
    }
}).catch(function(error) {
    console.log('error', error);
});
console.log('data', opticalIp);

const env = {
    API_URL: `${protocol}://${opticalIp}/`,
    configHeaders: ''
};
  
module.exports = env;

Here I am fetching the data from other server and also here I am returning some env value to other file. In my case before response the env object is returning so that the API_URL value is going blank. Here I need after the success response the API_URL will be prepared and then this env object should return to other file where ever it is called. Please help me to get the proper solution.

CodePudding user response:

In this case, you'll want to just export a function that you can call from the other file. This will avoid quite a bit of headache later on. Since axios returns a promise (hence the .then()) call after the request, you can just return the axios request call and modify whichever value you'd like to return from within the .then() call.

Here's an overview of promise chaining provided by javascript.info.

// CHILD MODULE

const axios = require('axios');

function fetchOptical () {
  let protocol = '';
  let opticalIp = '';
  
  return axios.get(`https://10.0.12.17:9201/api/v1.0/settings/custom-form/Optical`)
    .then(function(response) {
      //console.log('succ', response);
      if(response.data && response.data.data) {
          protocol = response.data.data['scheme'];
          opticalIp = response.data.data['optical_ip'];
          const env = {
            API_URL: `${protocol}://${opticalIp}/`,
            configHeaders: ''
          };
          
          console.log('data', opticalIp);
          return env
      }
    }).catch(function(error) {
        console.log('error', error);
        return error
    });
  })
}

module.exports = fetchOptical;

// PARENT MODULE

import child from './child'

child().then(env => {

})

CodePudding user response:

Modifying protocol and opticalIp will not change API_URL. Instead, in the then, try:

if(response.data && response.data.data) {
    const protocol = response.data.data['scheme'];
    const opticalIp = response.data.data['optical_ip'];
    env.API_URL = `${protocol}://${opticalIp}/`;
}

// Then when defining env:

const env = {
    API_URL: null, // null or '' or whatever you want the default to be.
    configHeaders: ''
};
  • Related