When querying a table in my database using the find() method, I am returned with a record that includes a column containing an array of user IDs.
My goal is to iterate through these IDs and use another find() method to retrieve the details of each user.
Based on my understanding, I believe I need to implement a promise to ensure that the loop completes before proceeding. However, I am unsure of how to correctly integrate a promise in this scenario.
Despite attempting the following code, both console logs are showing empty arrays:
getShortList: (req, res) => {
function shortList(ids) {
var companies = []
for (var i = 0; i < ids.length; i++) {
await new Promise(resolve => {
setTimeout(() => {
Company.find({ id: ids[i] }).exec((err, company) => {
if (err) return res.serverError(err)
companies.push(company)
})
}, 5000)
resolve(companies)
})
}
}
async function testFunc() {
var job_type = req.body.job_type
var job_listing_id = req.body.job_listing_id
Supplier_job_listing.find({ id: job_listing_id }).exec((err, job) => {
if (err) return res.serverError(err)
var list = shortList(job[0].shortlisted_bidders_ids)
list.then((result) => {
console.log('receiving it here', result)
})
})
}
testFunc();
}