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

216
Views
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 answers
Answer question

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 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!