我在我的 Strapi 中有一个函数,我可以在其中映射数据并在其中运行另一个查询。
async search(ctx) {
const authors = [
{
id: 1,
name: "author 1"
},
{
id: 2,
name: "author 2"
},
]
authors.map((data, i) => {
var books = this.getBooks(data.id)
console.log(books)
// some stuffs here
})
},
getBooks: async function (authorId) {
return await strapi.query('books').find({ userId: authorId })
}
console.log('books')
显示 Promise { <pending> }
。我对承诺的东西不是很熟悉,但我尝试了下面的方法,但我猜这不是正确的方法。它仍然返回相同的未决承诺。
getBooks: async function (authorId) {
const books = await strapi.query('books').find({ userId: authorId })
return Promise.resolve(books)
}
答案 0 :(得分:0)
发现此讨论 here。 map
似乎不适用于承诺。我切换到讨论中提到的 for
循环,现在可以正常工作了。
async search(ctx) {
const authors = [
{
id: 1,
name: "author 1"
},
{
id: 2,
name: "author 2"
},
]
for (let data of authors) {
const books = await strapi.query('books').find({ userId: data.id })
console.log(books)
// some stuffs here
}
}