I have a function in my strapi where I map the data and run another query inside.
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 })
}
The console.log('books')
display Promise { <pending> }
. I'm not very familiar with the promise stuff but I tried something like below but guess it's not the right way. It's still returning the same promise pending.
getBooks: async function (authorId) {
const books = await strapi.query('books').find({ userId: authorId })
return Promise.resolve(books)
}
Found this discussion here . It appears that map
doesn't work with promises. I switched to for
loop as mentioned in the discussion and it's working now.
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
}
}