Another problems with async / await

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. :slight_smile: :man_facepalming:

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 = ${} 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 (company) =>  {
      let res = await strapi.query('companies').findOne({'id': company.company_id}); = 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. :smiley: Maybe someone has an hint for me.

Thank you!


I think part of the issue might be that you are doing as assignment to inside an anonymous map. I’m not sure what sort of results this would actually led to. If you assigned it to a variable at the higher level (outside the for loop) then you might have better luck.

Hi, thanks for your input.

const entities = await (company) =>  {
    return await strapi.query('companies').findOne({'id': company.company_id});

But then in console.log(entities) I get:

[ Promise { <pending> }, Promise { <pending> }, Promise { <pending> } ]


Ahhhhh, I got it…

I’m waiting for an array of promises than a promise. So the function returns an array of Promise objects instead of a Promise. I have to use the function Promise.all

return await Promise.all( (company) =>  {
    return await strapi.query('companies').findOne({'id': company.company_id});

So, the final solution is:

let entities =  await Promise.all( (company) =>  { = await strapi.query('companies').findOne({'id': company.company_id});