System Information
- Strapi Version: 3.5.6
- Operating System: localhost (Mac OSX 11.2.2 (20D80))
- Database: 10.5.6-MariaDB-1:10.5.6
- Node Version: 14.6.1
- NPM Version: 6.14.12
- Yarn Version: 1.22.10
Hi guys,
another problem.
In my company controller I’ve got the following code:
const { sanitizeEntity } = require('strapi-utils');
module.exports = {
async getCompanisByUser(ctx) {
let companies;
const user = ctx.state.user;
if (!user) {
return ctx.badRequest(null, [{ messages: [{ id: 'No authorization header was found' }] }]);
}
let sql = `SELECT *
FROM companies_users__users_companies
WHERE user_id = ${user.id} AND confirmed = 1`
let results = await strapi.connections.default.raw(sql)
companies = results[0]
// Iterate over companies to get more information about them
for await (const company of companies.map(async (company) => {
let res = await strapi.query('companies').findOne({'id': company.company_id});
company.company = await sanitizeEntity(res, {
model: strapi.models['companies'],
});
}))
return companies
}
};
For example I’ve got 3 companies, where confirmed
is 1. So, the problem is in the for-loop. I want to get more information about the company. When I execute it by Postman first, e.g. the first of three results have more information about the company. When I execute it the second time, e.g. the first and the third result have more information.
I think there’s something wrong with my await / async functions. But I don’t get it. Maybe someone has an hint for me.
Thank you!
Best,
Thorsten