I am performing a database query and then processing the data in order to produce a single numerical value. This value needs to be used in subsequent arrays.
The array definition currently looks like this (below). The problem is that all_kdm_coverage
is a promise. But what I need to do is extract the numerical value from the promise.
const scrolls = [ { title: "Kadena de Mano", link: "./Kdm", coverage: all_kdm_coverage }, ]
This is a larger code snippet to provide more context
async function GetCoverage(scroll_path) { const apiName = 'foo'; const path = '/scrolls/' + scroll_path; const myInit = { headers: {}, response: false, }; const response = await API.get(apiName, path, myInit) console.log('response:',response) return response.Items } function GetScroll(scroll_name, scroll_array) { const scroll_data = scroll_array.find(scroll => scroll.Scroll === scroll_name); console.log('scroll_data:',scroll_data) return scroll_data } let dataGlobal; const getData = () => (dataGlobal ??= GetCoverage("all")); const all_kdm_coverage = getData().then( (data) => { const kdm_entry = GetScroll("kdm", data) const coverage = kdm_entry.Coverage console.log('kdm coverage:',coverage) return coverage } ) const scrolls = [ { title: "Kadena de Mano", link: "./Kdm", coverage: all_kdm_coverage }, ] // For completeness, here is how the scrolls array gets used export const DanzanRyuCollection = () => { return ( <> <h1 className="h1_index">Scrolls</h1> <Collection type="list" items={scrolls} gap="1.0rem" alignItems="center"> {(item, index) => ( <ScrollCard key={index} padding="1rem"> <Link href={item.link}> {item.title} </Link> <Rating ratings={[item.coverage]}/> </ScrollCard> )} </Collection> </> ); };
What is the best way to set the coverage:
value in scrolls
?
renew: It is recommended to perform the following operations
const scrolls = [ { title: "Kadena de Mano", link: "./Kdm", coverage: await all_kdm_coverage }, ]
But this results in the following error, indicating that it is experimental and not fully supported
Module parse failed: The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enabled it) Error: The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enabled it) console.error @ client.js:1 window.console.error @ next-dev.js:27 overrideMethod @ react_devtools_backend_compact.js:2367 handleErrors @ hot-dev-client.js:131 processMessage @ hot-dev-client.js:202 eval @ hot-dev-client.js:50 eval @ websocket.js:58 handleMessage @ websocket.js:57```
P粉7446912052024-04-03 12:40:58
ExpandBergi's comments
const scrolls = []; all_kdm_coverage.then(coverage => { scrolls.push({ title: "Kadena de Mano", link: "./Kdm", coverage, }) })