0

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

enzo
  • 419
  • 3
  • 7
  • 16
  • 1
    You didn't return anything from `getchallengeTokenVtiger` so its promise resolved to undefined. – Kevin B May 18 '22 at 20:30
  • try to add `return` before the `axios.get(...)` (and you don't have to make that method `async`). Then your function will return the Promise that resolves to the md5 token (as opposed to resolving to undefined as you currently have) – Robin Zigmond May 18 '22 at 20:30

1 Answers1

1

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()
    });
}
bturner1273
  • 660
  • 7
  • 18