Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
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});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

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"
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda