0

So, I ran into a weird bug in javascript.

const obj1 = await chartService.callAPIConversion({ unix_timestamp: Math.floor(activity[0].createdAt.getTime() / 1000) })

console.log(obj1.usd_rate);
//THIS WORKS -> returns proper value

console.log(await (chartService.callAPIConversion({ unix_timestamp: Math.floor(activity[0].createdAt.getTime() / 1000) })).usd_rate);
//THIS DOESNOT WORK -> returns undefined

Why am I not able to get the key without using a variable?

dexter
  • 27
  • 5
  • 5
    `(await`…`).usd_rate`, not `await (`…`).usd_rate`. `.` has higher precedence than `await`. – Sebastian Simon Jan 26 '22 at 14:59
  • Related: [Understanding await with Javascript array indexing](/q/66050816/4642212), [What is the Operator Precedence of Await?](/q/48218744/4642212), and some of their linked posts, though these could use some improvement… – Sebastian Simon Jan 26 '22 at 15:07

1 Answers1

0

You're not able to access it without creating a variable because your are retrieving values from a JavaScript object not an API call. Once the JS object called obj1 gets the informations related to your API call you can now access these informations threw the call of your JS object.

  • The actual issue was actually due to operator precedence as mentioned in the first comment of the post. Thanks anyways. – dexter Jan 26 '22 at 15:32