我正在嘗試以這種方式從資料庫中獲取資料:我想要所有模板,并且這些模板中有一個類別陣列,所以
templateArray = [
template1 = { name:string, ..., categories: array}
template2 = { name:string, ..., categories: array}
]
我目前使用的方法
const findAll = async () => {
let template = await getKnex()(tables.template).select();
template.forEach(async (value) => {
const categories = await getKnex()(tables.template)
.select()
.where(`${tables.template}.templateId`, value.templateId)
.join(
tables.template_category,
`${tables.template}.templateId`,
'=',
`${tables.template_category}.template_id`,
)
.join(
tables.category,
`${tables.category}.catId`,
'=',
`${tables.template_category}.cat_Id`,
);
value.categories = categories;
});
return template;
};
目前我正在這樣做,但物件似乎沒有改變。
uj5u.com熱心網友回復:
你不是在等待承諾解決。你應該做:
await Promise.all(template.map(yourFunction))
代替
template.forEach(yourFunction)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/476292.html
標籤:javascript sql api knex.js 考阿
