我正在执行数据库查询,然后处理数据,以便生成单个数值。该值需要在后续数组中使用。
数组定义当前如下所示(如下)。问题是 all_kdm_coverage
是一个承诺。但我需要做的是从承诺中提取数值。
const scrolls = [ { title: "Kadena de Mano", link: "./Kdm", coverage: all_kdm_coverage }, ]
这是一个更大的代码片段,可提供更多上下文
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> </> ); };
在 scrolls
中设置 coverage:
值的最佳方法是什么?
更新: 建议执行以下操作
const scrolls = [ { title: "Kadena de Mano", link: "./Kdm", coverage: await all_kdm_coverage }, ]
但这会导致以下错误,表明其处于实验阶段且不受完全支持
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
展开Bergi的评论
const scrolls = []; all_kdm_coverage.then(coverage => { scrolls.push({ title: "Kadena de Mano", link: "./Kdm", coverage, }) })