Ahmad Othman Ahmad Othman - 3 years ago 221
Javascript Question

How do I scrap down a somewhat JSON-looking variable?

I am trying to make a Node.js bot, so I found a module, installed it and tried out its example code.
Now, I have an async function that loads some text from an API. This is the code:

(async () => {
// Display user's balance
balance = kraken.api('Balance');
console.log(await balance);

When I run the code above, this is what I get in commandprompt:

error: [],
result: { A: '2.0', BC: '0.005', BCA: '111' }

How would I be able to make it only log a specific part of this, which looks like an array?
I've tried doing stuff like (to get it to return 2.0):

console.log(await balance.result.A)

But that does not seem to work as it returns this:

(node:6604) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'A' of undefined
(node:6604) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Out of ideas and need help.. Thanks!

Answer Source

You habe to put your await statement in parentheses, like this:

console.log((await balance).result.A);

This will fetch the balance asynchronously, then log the result property.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download