Quisiera mostrar solo un alumno.pero hay el error de decirme que no es una funcion
Código actual
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); },
El es el resultado del código actual.
[ { "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" } ]
Después del cambio
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); },
Luego mostrará el error que no es una función.
error: Sending 500 ("Server Error") response: TypeError: #<Object> is not a function at Array.find (<anonymous>) at Object.detail [as event/detail]
El código que edité está abajo.
var participants = await event.participants.find({id:3});
Refiero el sitio web aquí para modificar mi código. El resultado es correcto ahora.
https://www.codegrepper.com/code-examples/javascript/array.find+find+is+not+a+function
var participants = await event.participants.find(({ id }) => id === 3);
El resultado
{ "createdAt": 1636793903089, "updatedAt": 1636793903089, "id": 3, "role": "student", "username": "student1" }