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

161
Views
Mongoose !findOne

Why is it that my first statement always returns false when I make up a code that doesn't exist in the "Courses" collection?

// POST /api/my/courses
app.post('/api/my/courses', function(req, res) {
  let courseCode = req.body.courseCode.toUpperCase();
  let status = req.body.status;

  if (!Courses.findOne({ courseCode })) {
    res.status(404).send({ "message": "Course not found." })
  } else {
    let course = new MyCourses({
      courseCode: courseCode,
      status: status
    });

    course.save(function(err) {
      if(err) return console.error(err);
    });

    res.status(201).send({ "message": "New course added." });
  }
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should await for findOne and save as they both return promises. Or, you can also pass in a callback function to findOne as the 2nd argument. Refer to this

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!