0

I was able to run this script and get the values out of the response. Happy as I was, I wrapped the script in a function and then it returned UNDEFINED. I am pulling all my hairs because the script without the function returns values in the consolelog but the function does not. ANy help is very appreciated.

\ SCRIPT

var tokenaddress = '2G3Gm6yZ2ZYu5B5rT7Fp8M1PyxdFAuhQ9VUhGTyBQYUJ'
var site = 'https://gmgn.ai/defi/quotation/v1/tokens/sol/';

var tokenurl = site + tokenaddress;
var graphurl = 'https://gmgn.ai/sol/token/' + tokenaddress;

var axios = require('axios');
axios.get(tokenurl)
    .then(function (response) {
    
    token = (response.data).data.token;
    var tokenName = token.name;
    var tokenLiquidity = token.liquidity;
    
    
    console.log('TokenName: ', tokenName,  'tokenLiquidity: ', tokenLiquidity);
});


OUTPUT: TokenName: Kim Kardashian tokenLiquidity: 0.21558013114

When I wrap this script in a function it returns UNDEFINED. why I do not understand since I get values in de consolelog

\ FUNCTION

function FnGetToken(tokenaddress) {
var site = 'https://gmgn.ai/defi/quotation/v1/tokens/sol/';

var tokenurl = site + tokenaddress;
var graphurl = 'https://gmgn.ai/sol/token/' + tokenaddress;

var axios = require('axios');
axios.get(tokenurl)
    .then(function (response) {
    
    var token = (response.data).data.token
    var tokenName = token.name
    var tokenLiquidity = token.liquidity; 
    
    return  tokenName, tokenLiquidity
    
}
    
)
    
}
console.log(FnGetToken('2G3Gm6yZ2ZYu5B5rT7Fp8M1PyxdFAuhQ9VUhGTyBQYUJ'));

OUTPUT: undefined

read about undefined but couldnt make the function to work

1
  • In order to provide a more focused answer for your question, please share which environment you're running. Specially which Operating System you're using to run such scripts. Commented Jun 30 at 2:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.