I was making a backend application to schedule company visits and when I tried to use Mongoose 'populate' function, got this error:
TypeError: booking.execPopulate is not a function
I don't know what happened but this is my code:
BookingController.js
const Booking = require('../models/Booking')
module.exports = {
async store(req, res) {
const { user_id } = req.headers;
const { spot_id } = req.params;
const { date } = req.body;
const booking = await Booking.create({
user: user_id,
spot: spot_id,
date,
});
await booking.populate('spot');
await booking.populate('user');
await booking.execPopulate();
return res.json(booking);
}
}
BookingModel.js
const mongoose = require('mongoose');
const BookingSchema = new mongoose.Schema({
date: String,
approved: Boolean,
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
spot: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Spot'
}
});
module.exports = mongoose.model('Booking', BookingSchema);