Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

300
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda