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

200
Views
Problem from mongoose: populate is not a function

I have this code to create a new registration on RegistrationController, and the Registration Model

const Registration = require('../models/Registration');

module.exports = {
  async create(request, response) {
    const { user_id } = request.headers;
    const { eventId } = request.params;
    const { date } = request.body;

    const registration = await Registration.create({
      user: user_id,
      event: eventId,
      date
    });

    await registration
      .populate('event')
      .populate('user', '-password')
      .execPopulate(); //PROBLEM HERE

    return response.json(registration);
  },
const mongoose = require('mongoose');

const RegistrationSchema = new mongoose.Schema({
  date: String,
  approved: Boolean,
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User"
  },
  event: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Event"
  }
});

module.exports = mongoose.model('Registration', RegistrationSchema);

When i send a post request to insomnia, this error is shown: (node:6260) UnhandledPromiseRejectionWarning: TypeError: registration.populate(...).populate is not a function. Can somebody help?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need .find or .findOne, it will retrieve and populate from the DB the document that was just created :

await Registration
      .findOne({user: user_id})
      .populate('event')
      .populate('user', '-password')
      .lean() // faster, returns simple JSON
      .exec(); // returns a true Promise
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!