Can anyone help me with a coding issue I'm facing? I have a constant called data
, which contains two values - prediction
and probability
. What is the best way to access and retrieve both values?
type ML = {
prediction: string;
probability: number;
};
type GetUsersResponse = {
data: ML[];
};
private async something(txt: string) {
try {
// 👇️ const data: GetUsersResponse
const { data, status } = await axios.post<GetUsersResponse>(
'http://127.0.0.1:5000/prediction',
{
"text": txt
},
);
console.log(JSON.stringify(data, null, 4));
// 👇️ "response status is: 200"
console.log('response status is: ', status);
return data
} catch (error) {
if (axios.isAxiosError(error)) {
console.log('error message: ', error.message);
return error.message;
} else {
console.log('unexpected error: ', error);
return 'An unexpected error occurred';
}
}
}
const data = something("This is a string")
// data looks like
// data = {
"prediction": "Class 1",
"probability": 0.612535596
}
This is what I attempted but encountered errors:
data[0].prediction
TS7053: Element implicitly has an 'any' type because expression of type '0' can't be used to index type 'string | GetUsersResponse'. Property '0' does not exist on type 'string | GetUsersResponse'.
data.prediction
TS2339: Property 'prediction' does not exist on type 'string | GetUsersResponse'. Property 'prediction' does not exist on type 'string'.
data.data[0].probability
TS2339: Property 'data' does not exist on type 'string | GetUsersResponse'. Property 'data' does not exist on type 'string'.