Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
Export value after axios call response using Node.js

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(`www.mydomain.com/api`).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.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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 => {

})

about 4 years ago · Juan Pablo Isaza Report

0

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: ''
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!