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

Hello!

Did you get it solved?
It sounds like serialization issue. Might be worth to try to use response directly to send data and see if it works
https://adonisjs.com/docs/4.1/response

You should return a JSON response not just returning control.

async getOrdersToCompany({ request, response }) {
  const { company_id } = request.get();
  
  try {
    if (isNaN(company_id)) {
      // Return bad request status code (400)
      return response.status(400).json(
        { 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 data as JSON response
      return response.status(200).json(
        { data: groupOrders, message: "Query successful" }
        )
      } catch (error) {
        return response.status(500).json(
          { error, message: "Internal server error" }
          )
        }
      }

Learn about status codes here:
https://www.restapitutorial.com/httpstatuscodes.html

Did not work.

{
  "data": [],
  "message": "Query successful"
}

Not! I really need that help with something that really works. It must be the logic that is not 100%.

Do you have any up or downstream middleware that might do something?

I don’t have any middleware.

it didn’t work for me.

It sounds like there’s something else off. Something that’s not visible in shared code
Perhaps some typo or some configuration or something

Can you share minimal Git repo to reproduce this?

1 Like