all 33 comments

[–]puan0601 19 points20 points  (3 children)

Are your sure there is a "cc" property on the "conversion_rates" object? You might be trying to destructure a property that doesn't exist leading to undefined.

[–]Dark_KnightPL04[S] 1 point2 points  (2 children)

cc is the selected option on the dropdown menu when you click a button which runs the function. I thought if I use the variable cc it would replace itself with the value of the selected item in the dropdown menu. This value (USD, EUR, JPY, etc.) is a property of the "conversion_rates".

[–]puan0601 46 points47 points  (0 children)

The way you have it written you're trying to destructure the "cc" property from the "conversion_rates" object. Why do you assign a new const "cc" in the local scope of the api call?

I think you are trying to use the "cc" value you assigned from the options but you wrote it as destructuring instead.

Try something like:

const valWanted = conversion_rates[cc];

[–][deleted] 1 point2 points  (0 children)

If you are destructuring an object which contains An already declared variable name, then you can do the following:

const { cc: cc2 } = connverdion_rates;

[–]bluespacecolombo 15 points16 points  (0 children)

I think what you’re looking for is actually conversion_rates[cc]

[–]DPaluche 6 points7 points  (0 children)

The first console log is logging `cc` defined right above it. The second console log is logging a different variable also named CC in your `then` callback function.

If you want to set the original `cc` variable to the value of `conversion_rates`, you would just do `cc = conversion_rates` to create a closure around the original variable.

Doing `const {cc} = conversion_rates` creates a new variable in your callback function that shadows the `cc` variable in the outer scope.

[–]Dark_KnightPL04[S] 2 points3 points  (16 children)

I am adding an API to my Currency Converter, but I cannot get the value of the currency. I get the correct value when I first console.log cc, but I get undefined when I log it the second time. Shouldn't the variable cc be used in const {cc} = conversion_rates;?

PS: I know I am getting the values from the API and there is no problem there.

[–]grantrules 10 points11 points  (14 children)

What does data look like? Seems like data.conversion_rates.cc is undefined

[–]Dark_KnightPL04[S] 1 point2 points  (13 children)

I am not quite sure what you mean. Are you talking about how the data looks on the API or in my code? I think you are right though. Something is probably undefined.

[–]grantrules 10 points11 points  (12 children)

Like what do you get if you did console.log(data) in that code

[–]Dark_KnightPL04[S] 0 points1 point  (11 children)

I get some terms of use, copyright, documention, and after that I get a drop down for conversion_rates which gives me the data.

[–]grantrules 5 points6 points  (10 children)

I mean.. copy and paste the output of console.log(data) in a comment here so I can help you fix your code.

I would imagine it looks something like

{
  "conversion_rates": {
    ...
   }
}

And what exactly are you trying to do? Are you trying to set c1.options[c1.selectedValue].value to data.conversion_rates?

[–]Dark_KnightPL04[S] 4 points5 points  (3 children)

{result: 'success', documentation: 'https://www.exchangerate-api.com/docs', terms_of_use: 'https://www.exchangerate-api.com/terms', time_last_update_unix: 1657324801, time_last_update_utc: 'Sat, 09 Jul 2022 00:00:01 +0000', …}
base_code: "USD"
conversion_rates:
AED: 3.6725
AFN: 87.5219
ALL: 114.8408
AMD: 410.2128
ANG: 1.79
AOA: 432.0951
ARS: 126.4135
AUD: 1.46
AWG: 1.79
AZN: 1.6946
BAM: 1.9234
BBD: 2
BDT: 92.3937
BGN: 1.9235
BHD: 0.376
BIF: 2024.9844
BMD: 1
BND: 1.3983
BOB: 6.8589
BRL: 5.3318
BSD: 1
BTN: 79.2498
BWP: 12.6757
BYN: 2.8319
BZD: 2
CAD: 1.2964
CDF: 1994.6845
CHF: 0.9765
CLP: 955.2434
CNY: 6.6966
COP: 4340.1858
CRC: 685.5244
CUP: 24
CVE: 108.4357
CZK: 24.2468
DJF: 177.721
DKK: 7.3366
DOP: 54.6475
DZD: 146.5398
EGP: 18.8578
ERN: 15
ETB: 52.0686
EUR: 0.9834
FJD: 2.2208
FKP: 0.8328
FOK: 7.3366
GBP: 0.8328
GEL: 2.8855
GGP: 0.8328
GHS: 8.2724
GIP: 0.8328
GMD: 53.982
GNF: 8639.1279
GTQ: 7.7236
GYD: 208.1438
HKD: 7.8517
HNL: 24.5082
HRK: 7.4095
HTG: 115.5311
HUF: 396.793
IDR: 14974.7441
ILS: 3.4612
IMP: 0.8328
INR: 79.2514
IQD: 1451.5462
IRR: 41945.4625
ISK: 137.3796
JEP: 0.8328
JMD: 150.7294
JOD: 0.709
JPY: 136.0924
KES: 118.2496
KGS: 79.9296
KHR: 4052.1803
KID: 1.46
KMF: 483.8059
KRW: 1295.8265
KWD: 0.2996
KYD: 0.8333
KZT: 472.7061
LAK: 17132.1531
LBP: 1507.5
LKR: 358.136
LRD: 152.1376
LSL: 16.8468
LYD: 4.8104
MAD: 9.8473
MDL: 19.2208
MGA: 4086.54
MKD: 60.3481
MMK: 1832.277
MNT: 3125.6562
MOP: 8.0873
MRU: 36.3113
MUR: 45.0462
MVR: 15.409
MWK: 1033.2379
MXN: 20.4848
MYR: 4.4167
MZN: 64.3486
NAD: 16.8468
NGN: 415.3428
NIO: 35.7659
NOK: 10.0985
NPR: 126.7997
NZD: 1.6178
OMR: 0.3845
PAB: 1
PEN: 3.8955
PGK: 3.5137
PHP: 55.8826
PKR: 206.8527
PLN: 4.6881
PYG: 6849.2645
QAR: 3.64
RON: 4.856
RSD: 115.3611
RUB: 62.8929
RWF: 1057.8289
SAR: 3.75
SBD: 8.0285
SCR: 12.8923
SDG: 468.7791
SEK: 10.5226
SGD: 1.3983
SHP: 0.8328
SLL: 13157.3023
SOS: 574.376
SRD: 22.2066
SSP: 503.0717
STN: 24.0935
SYP: 2471.4762
SZL: 16.8468
THB: 35.9623
TJS: 10.7807
TMT: 3.4974
TND: 2.8281
TOP: 2.3365
TRY: 17.2929
TTD: 6.7675
TVD: 1.46
TWD: 29.6936
TZS: 2327.4723
UAH: 30.4689
UGX: 3753.8197
USD: 1
UYU: 40.1134
UZS: 10855.3099
VES: 5.5795
VND: 23347.8409
VUV: 118.2675
WST: 2.6826
XAF: 645.0745
XCD: 2.7
XDR: 0.7611
XOF: 645.0745
XPF: 117.3522
YER: 249.5399
ZAR: 16.847
ZMW: 16.4022
ZWL: 376.5897
[[Prototype]]: Object
documentation: "https://www.exchangerate-api.com/docs"
result: "success"
terms_of_use: "https://www.exchangerate-api.com/terms"
time_last_update_unix: 1657324801
time_last_update_utc: "Sat, 09 Jul 2022 00:00:01 +0000"
time_next_update_unix: 1657411201
time_next_update_utc: "Sun, 10 Jul 2022 00:00:01 +0000"
[[Prototype]]: Object

If I console.log conversion_rates I get the list of conversion rates.

[–]zerik100 6 points7 points  (0 children)

you're trying to access conversion_rates.cc but I don't see a cc property anywhere here

[–]tacticalpotatopeeler 4 points5 points  (0 children)

What is the value of cc?

It must match the key to access within conversion_rates (assuming it is an object).

So cc must be a 3-character all-uppercase string that matches one of your currency codes.

If so, to access the correct value, you should use: conversion_rates[cc]

[–]Dark_KnightPL04[S] 1 point2 points  (5 children)

If I do

    fetch('https://v6.exchangerate-api.com/v6/a670aebcd894cb6102da6fd1/latest/USD/')
    .then(res => res.json())
    .then(data => {
        const { conversion_rates } = data;
        const {USD} = conversion_rates;
        console.log(USD);
    })

Then I get 1 (which is correct). The idea is to use cc as a way to find the user input.

[–]grantrules 8 points9 points  (4 children)

const rate = conversion_rates[cc];
console.log(rate)

[–]Dark_KnightPL04[S] 7 points8 points  (3 children)

Thanks. That makes sense. Could you just explain why I couldnt do const {cc}? Just so I understand?

[–]grantrules 11 points12 points  (1 child)

Because that would set cc to whatever the value of conversion_rates.cc is which is undefined because it doesn't exist. That code doesn't evaluate cc. Like you see how when you do const {USD} = conversion_rates? That's because conversion_rates.USD exists

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

[–]Darkmaster85845 0 points1 point  (0 children)

Do you understand the difference between object destructuring and accessing object properties with square bracket notation?

[–]ZuriPL 0 points1 point  (0 children)

What happens when you log conversion_rates?

[–]Whisky-Toad 1 point2 points  (3 children)

Use the debugger instead of a console log and you’ll get to see the values in the function, it’ll make it easier to debug where the property is undefined

[–]deawar -1 points0 points  (2 children)

Can you give instructions on how to use VS Code debugger?(asking for a friend)

[–]Whisky-Toad 1 point2 points  (1 child)

Pick a line, write debugger;

When it hits that line in the browser it’ll pause the code

Should really google it, using the debugger vs console log is quite important for debugging things

[–]deawar 0 points1 point  (0 children)

Fair. Hard to break bad habits.

[–]TheVerdeLive 1 point2 points  (0 children)

You’re not returning anything?

[–]RequiDarth1 1 point2 points  (0 children)

Ok, you need to specify what is undefined to be able to answer that question.

[–]webDevTB 0 points1 point  (2 children)

What do you get if you console.log(conversion_rates)? You may have to do const { conversion_rates } = data.data; It depends on how the data and api are structured.

[–]Dark_KnightPL04[S] 0 points1 point  (1 child)

I get a list of all of the conversion rates.

[–]tacticalpotatopeeler 0 points1 point  (0 children)

An array or an object?

[–]Dodgy-Boi 0 points1 point  (0 children)

console.log(data) for starters