Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
¿Cómo iterar sobre el resultado de strapi.db.query?

Tengo una consulta simple de findMany en mi controlador:

 const appointments = strapi.db.query("api::appointment.appointment").findMany({ select: ["date", "startTime", "endTime"], where: { mentorId: mentorId, status: [1, 2, 4] } });

Devuelve lo siguiente, tal estructura me hizo pensar que el resultado devuelto es iterable.

 [ { "date":"2022-04-27", "startTime":"08:00:00.000", "endTime":"10:00:00.000" }, { "date":"2022-04-27", "startTime":"10:00:00.000", "endTime":"12:00:00.000" } ]

Sin embargo, cuando uso un bucle for , puedo decir que ni siquiera entró porque no hay salida.

 for (let appointment in appointments) console.log("I entered");

Cuando usé forEach , recibí error: appointments.forEach is not a function .

 appointments.foreach(function(appointment) { console.log(appointment.date); });

Entonces, verifiqué si es iterable usando Symbol.iterator in Object(appointments) , pero devuelve false . Array.isArray(appointments) también devuelve false , lo cual es extraño, porque el resultado definitivamente me pareció una matriz.

¿Hay alguna solución para esto? Necesito recorrer cada objeto y acceder a sus campos.

EDITAR - Todo el archivo del controlador:

 "use strict"; /** * availability controller */ const { createCoreController } = require("@strapi/strapi").factories; module.exports = createCoreController("api::availability.availability", ({strapi}) => ({ async getFreeSlots(ctx) { const mentorId = ctx.params.mentorId; const appointments = strapi.db.query("api::appointment.appointment").findMany({ select: ["date", "startTime", "endTime"], where: { mentorId: mentorId, status: [1, 2, 4] } }); console.log(appointments); return appointments; } }));
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El método .findMany() es asíncrono y devuelve una Promesa. Supongo que, en cambio, está tratando de usarlo sincrónicamente justo después de la llamada. Tienes que await .

 const appointments = await strapi.db.query("api::appointment.appointment").findMany({ select: ["date", "startTime", "endTime"], where: { mentorId: mentorId, status: [1, 2, 4] } }); console.log(appointments);

Alternativamente, si no está en una función async (que es necesaria para usar await ), puede usar el clásico .then() de Promise:

 strapi.db.query("api::appointment.appointment").findMany({ select: ["date", "startTime", "endTime"], where: { mentorId: mentorId, status: [1, 2, 4] } }).then((appointments) => { console.log(appointments); /*...*/ });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!