I want to get CAD value from https://api.exchangeratesapi.io/latest
I have already used so many types of code, and it says that "TypeError: Cannot read property 'CAD' of undefined"
Really need your help, thank you very much.
It outputted all the Currencies if I console this code
((this.state.data as any).rates)
but when i want to get CAD currencies, it says the error
I have tried these codes :
((this.state.data as any).rates as any).CAD
(this.state.data as any)["Rates"]["CAD"];
(this.state.data as any)["Rates"].CAD;
The way I get the data is
interface IState {
data?: object | null;
isCurrency?: boolean;
Currency?: string;
Rate?: number;
}
export default class Header extends Component<{}, IState> {
service: UserService = new UserService();
state = {
isCurrency: false,
Currency: "USD",
Rate: 1,
data: [] = []
};
async componentDidMount() {
let result = await this.service.getAllCurrency();
this.setState({
data: (result as Pick<IState, keyof IState>).data
});
console.log(result);
}
}
1.4591 (Based on the latest API)
any, you probably have a problem 2. examine the data in the debugger or with aconsole.log. It seems like the data is not in the place you think it is.