Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

301
Vistas
NodeJS return value in axios get

I have this class written in javascript but I have difficulty in obtaining the result from the axios request, below my situation to better explain the problem:

i have a file called vtiger.js in the classes directory in the project root

vtiger.js

const axios = require('axios');
var md5 = require('md5');
var qs = require('qs');
const https = require('https');

class vTiger {

    constructor() {
        this.url = process.env.VTIGER_URL;
        this.username = process.env.VTIGER_USERNAME;
        this.password = process.env.VTIGER_PASSWORD;
    }

    async getchallengeTokenVtiger() {
        var token;
        var tokenmd5 = false;

        var url = this.url + 'webservice.php?operation=getchallenge&username=' + this.username;

        axios.get(url,
            {
                headers: {
                    "content-type": "application/x-www-form-urlencoded"
                },
                httpsAgent: new https.Agent(
                {
                    rejectUnauthorized: false
                })
            }).then(response => {
            if (response.data.success) {
                token = response.data.result.token;
                tokenmd5 = md5(token + this.password);
                return tokenmd5;
            }
        });
    }
}

module.exports = vTiger

then I have a file called api.js in the controllers folder with this content:

const http = require('http');
const axios = require('axios');
var qs = require('qs');

const vTiger = require('../classes/vtiger');

exports.welcome = (req, res, next) => {

    const vtigerClass = new vTiger();

    console.log(vtigerClass.getchallengeTokenVtiger())

    res.status(200).json({
        data: vtigerClass.getchallengeTokenVtiger()
    });
}

from this file as an response I get:

{
    "data": {}
}

while from the console.log(vtigerClass.getchallengeTokenVtiger()) line I get this:

Promise { undefined }

Where am I doing wrong?

Thanks

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

you probably don't want to use the .then in an async function. you should

const response = await axios.get(...)
if (response.data.success) {
    token = response.data.result.token;
    tokenmd5 = md5(token + this.password);
    return tokenmd5;
}
else return null;

Or you can create a variable in your function called tokenmd5 i.e.

let tokenmd5 = ''

set its value in the .then, then return tokenmd5 at then end of your function NOT in the .then

THEN: for your console.log you want:

exports.welcome = async (req, res, next) => {

    const vtigerClass = new vTiger();

    console.log(await vtigerClass.getchallengeTokenVtiger())

    res.status(200).json({
        data: vtigerClass.getchallengeTokenVtiger()
    });
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda