Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

80
Vistas
Sails.js Find where error javascript by id

I would like to show only one student.but there is the mistake that tell me that is not a function


Current Code

detail: async function (req, res) {
    var thatEvent = await Event.findOne(req.params.eventid);
    if (!thatEvent) return res.notFound();

    var event = await Event.findOne(req.params.eventid).populate(
      "participants"
    );
    if (!event) return res.notFound();
    var participants = await event.participants;
    return res.json(participants);
  },

The is the result of the current code

[
  {
    "createdAt": 1636793903089,
    "updatedAt": 1636793903089,
    "id": 3,
    "role": "student",
    "username": "student1"
  },
  {
    "createdAt": 1636793903089,
    "updatedAt": 1636793903089,
    "id": 4,
    "role": "student",
    "username": "student2"
  },
  {
    "createdAt": 1636793903089,
    "updatedAt": 1636793903089,
    "id": 5,
    "role": "student",
    "username": "student3"
  }
]



After change

 detail: async function (req, res) {
    var thatEvent = await Event.findOne(req.params.eventid);
    if (!thatEvent) return res.notFound();

    var event = await Event.findOne(req.params.eventid).populate(
      "participants"
    );
    if (!event) return res.notFound();
    var participants = await event.participants.find({id:3});
    return res.json(participants);
  },

Then it will show the error that is not a function

error: Sending 500 ("Server Error") response: 
 TypeError: #<Object> is not a function
    at Array.find (<anonymous>)
    at Object.detail [as event/detail] 

The code I edited is below

 var participants = await event.participants.find({id:3});
7 months ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I refer the website here to amend my code. The result is correct now.
https://www.codegrepper.com/code-examples/javascript/array.find+find+is+not+a+function

var participants = await event.participants.find(({ id }) => id === 3);

The result

{
  "createdAt": 1636793903089,
  "updatedAt": 1636793903089,
  "id": 3,
  "role": "student",
  "username": "student1"
}
7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos