I write Python 99% of the time but decided to start playing with Amazon Cloudscape and therefore started to write some React. It's been a slog...
I have a backend service (flask) that returns a JSON object of data I want to use in order to create a line-chart (using the Cloudscape Linechart component). However, the JSON data is within a promise (Async fetch statements) and for the life of me I cannot figure out how to assign it to a variable that can be used as an export for my Linechart component class.
How do I:
- Get rid of the Promise object (parse it)
- Get a valid array in its own variable
- Be able to consume the variable (export) in a different class (e.g.,
import {my_var} from "./my_class").
I need to do some math on this data which complicates this.
Fetch Function that returns a Promise
export async function GetRegionRidecounts(region) {
try {
var response = await fetch('http://localhost:5000/get_region_ridecounts?region_name=' + region, {
method: 'GET',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
},
});
return await response.json()
} catch (error) {
return { "error": error };
}
}
Consumer function that returns a Linechart
export default function RidersLineChart(region) {
console.log(region)
GetRegionRidecounts("west-lake-marion-park")
.then(data => {
return <LineChart
height={200}
series={data}
xDomain={[data[0].date,data[data.length -1].data]}
yDomain={[0, 500]}
xScaleType="time"
xTitle="Time (UTC)"
yTitle="Data transferred"
ariaLabel="Network traffic"
ariaDescription={`Line chart showing transferred data of all your instances.`}
/>
});
}
Via Active questions tagged javascript - Stack Overflow https://ift.tt/W2IE7d1
Comments
Post a Comment