Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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()
    });
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading