0

The following code gives me undefined. I tried fixing it with various async/await but it came up as an error. Anyone know how to fix? EDIT: To be more specific, I need the value of "output" stored. I am using console.log just to guarantee it is getting the information I need it to have.

const URL = 'https://api.1inch.exchange/v3.0/1/quote?fromTokenAddress=0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE&toTokenAddress=0x6b175474e89094c44da98b954eedeac495271d0f&amount=1&protocols=';
function getAmount(){
    axios.get(URL).then((response) => {
    //console.log(response.data)
    const output = response.data.toTokenAmount
    //console.log(output)
    return output
})
//console.log(output)  
}
console.log(getAmount());
daredevil
  • 53
  • 5
  • Double check on the value of console.log(response.data) and go from there. Not at a PC at the moment. – boostedd Apr 17 '21 at 22:42
  • I did that's why the comments are there. The value is correct it just does not get passed out of the function correclty. – daredevil Apr 17 '21 at 22:47
  • Why not call the function instead of console.log the function? Maybe try const data = getAmount() then console.log(data) – boostedd Apr 17 '21 at 22:48
  • I tried calling the function as well, it just doesn't do anything. I also tried something similar to what you mentioned second. – daredevil Apr 17 '21 at 22:54
  • Does this answer your question? [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Jared Smith Apr 17 '21 at 22:55
  • Or possibly [this one](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). Or any of a hundred thousand others. – Jared Smith Apr 17 '21 at 22:56
  • @JaredSmith if you try those fixes on this problem you'll see it doesn't work – daredevil Apr 18 '21 at 00:11
  • It does. You aren't returning anything. Your return statement is in the *inner* function. Vejsil's answer (along with all the ones on the linked dupes) are correct. – Jared Smith Apr 18 '21 at 14:07

1 Answers1

0

You must return promise inside getAmountfunction. Also you need to wait on that promise when calling getAmount function. This should work:

const URL = 'https://api.1inch.exchange/v3.0/1/quote?fromTokenAddress=0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE&toTokenAddress=0x6b175474e89094c44da98b954eedeac495271d0f&amount=1&protocols=';
function getAmount(){
    return axios.get(URL).then((response) => {
    const output = response.data.toTokenAmount
    return output; })
}

getAmount().then(toTokenAmount => console.log(toTokenAmount));
Vejsil Hrustić
  • 179
  • 1
  • 7
  • 1
    This gives an output but the output isn't correct. I'm honestly not sure what the output it's giving is. – daredevil Apr 17 '21 at 22:56
  • @Vejsil You don't need the lambda, the `.log` method doesn't require the `console` context so you can pass it directly as a callback: `getAmount().then(console.log);` – Jared Smith Apr 17 '21 at 22:57
  • 1
    @daredevil maybe you need to return `response.data` when you receive response from API? @JaredSmith you're right. Nice suggestion, thank you. – Vejsil Hrustić Apr 17 '21 at 23:01
  • @VejsilHrustić I tried that does not work either – daredevil Apr 18 '21 at 00:12
  • @daredevil When you set `output` to `response.data` it still logs `undefined`? – Ace Apr 18 '21 at 04:51
  • @daredevil I’m not sure how you are running into an issue with the code in this answer. I copy and pasted it to an editor in my own machine and it works great. – boostedd Apr 18 '21 at 17:28
  • @boostedd did you check what the response is supposed to be? it looks like it works great but the response it gives is wrong. – daredevil Apr 26 '21 at 22:22
  • This is response complete response https://pastebin.com/UFDBE7FT . snippet is valid and it works. It's other issue if data you receive isn't s expected. In that case you should check what params are you sending to exchange API when fetching the data. – Vejsil Hrustić Apr 27 '21 at 07:26