Why doesn't it show results when I make a request

I have this method:

 async getOrdersToCompany({ request }) {
    const { company_id } = request.get();

    try {
      if (isNaN(company_id)) {
        return { error: true, message: "NOT_INTEGER" };
      }

      //The problem not is here
      const orders = await Order.query()
        .where("company_id", parseInt(company_id))
        .with("user")
        .with("company")
        .with("pedidos", (builder) => {})
        .with("status_clients", (builder) => {
          builder
            .where("active", 1)
            .orderBy("id", "desc")
            .setHidden(["updated_at"]);
        })
        .fetch();

      let ordersTemp = [],
        groupOrders = [];

      //A primeira coisa a se fazer é unir todos os elemetnos do array em um array temporário
      Object.values(orders.toJSON()).map((el, i) => {
        ordersTemp.push(el);
        groupOrders[el.status_clients[0].status_name] = [];
      });

      //Aqui, ordena-se o array
      ordersTemp.sort();

      //Aqui, preenche o array 'groupOrders'
      ordersTemp.forEach((el, i) => {
        //console.log(el.status_clients[0])
        groupOrders[el.status_clients[0].status_name].push(el);
      });
      //Perfect!
      console.log(groupOrders);

      //dont show nothing
      //The problem
      return groupOrders;
    } catch (error) {
      return error;
    }

For example, the expected result is similar to this
Show on the console, but not insomnia, there, it shows only an empty array like this: [ ]

info: serving app on http://192.168.129.171:3333
[
  entregando: [
    {
      id: 3,
      uuid: '63ebb6e6-c817-4f5b-bde1-b5f4f7e1c20f',
      notes: '',
      payback: 0,
      form_payment: '1',
      status: 'novo',
      company_id: 1,
      user_id: 1,
      created_at: '2020-05-15 23:50:45',
      updated_at: '2020-05-15 23:50:45',
      user: [Object],
      company: [Object],
      pedidos: [Array],
      status_clients: [Array]
    }
  ],
  aprovado: [
    {
      id: 4,
      uuid: '25606bec-fc59-424b-b4a9-9d116c448344',
      notes: '',
      payback: 0,
      form_payment: '1',
      status: 'novo',
      company_id: 1,
      user_id: 1,
      created_at: '2020-05-15 23:58:44',
      updated_at: '2020-05-15 23:58:44',
      user: [Object],
      company: [Object],
      pedidos: [Array],
      status_clients: [Array]
    },
    {
      id: 5,
      uuid: 'a50d653a-a705-498f-9f0f-2871c9f397a5',
      notes: '',
      payback: 0,
      form_payment: '1',
      status: 'novo',
      company_id: 1,
      user_id: 1,
      created_at: '2020-05-16 00:03:31',
      updated_at: '2020-05-16 00:03:31',
      user: [Object],
      company: [Object],
      pedidos: [Array],
      status_clients: [Array]
    }
  ],
  novo: [
    {
      id: 6,
      uuid: 'a50d653a-a705-498f-9f0f-2871c9f397a6',
      notes: '',
      payback: 0,
      form_payment: '1',
      status: 'novo',
      company_id: 1,
      user_id: 1,
      created_at: '2020-05-16 00:03:31',
      updated_at: '2020-05-16 00:03:31',
      user: [Object],
      company: [Object],
      pedidos: [],
      status_clients: [Array]
    },
    {
      id: 7,
      uuid: 'a50d653a-a705-498f-9f0f-2871c9f397a8',
      notes: '',
      payback: 0,
      form_payment: '1',
      status: 'novo',
      company_id: 1,
      user_id: 1,
      created_at: '2020-05-16 00:03:31',
      updated_at: '2020-05-16 00:03:31',
      user: [Object],
      company: [Object],
      pedidos: [],
      status_clients: [Array]
    }
  ]
]
1 Like

Hi @TaffarelXavier.

About your array handling I don’t have comments as I’m not a JS expert but you may avoid a lot of it if you call you orders quering over the status_clients:


const ordersByStatus = Status.query()
.with('orders',companyOrders=>{
  companyOrders.with('user')
   ....
}

another alternative is to use groupBy and groupByRaw.

B/R