【发布时间】:2021-05-25 16:18:15
【问题描述】:
我的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)
}
【问题讨论】:
-
尝试在 getBooks 函数中只返回书籍。
-
如果我只返回书籍,那与我在第一个代码块中的原始功能没有区别。