I have a model and a controller that are working almost perfectly. The only issue that I’m having is, I’m using populate for 2 fields, that both have 2 IDs, but only the first ID is being populated. I have done quite a bit of searching and have not found a solution. Below are the model and the controller.
Model
const mongoose = require(“mongoose”)
const TicketSchema = new mongoose.Schema({
title: {
type: String,
required: [true, "Ticket title is required"]
},
description: {
type: String,
required: [true, "Ticket description is required"]
},
assignedDev: {
type: String,
required: [true, "Assigned developer is required"]
},
submitter: {
type: String,
required: [true, "Submitter is required"]
},
ticketPriority: {
type: String,
enum: ["Lowest", "Low", "Medium", "High", "Highest"],
default: "Lowest"
},
ticketStatus: {
type: String,
enum: ["Open", "Resolved", "Closed", "Reopened", "In Progress"],
default: "Open"
},
ticketType: {
type: String,
enum: ["Bug", "New Feature", "Improvement", "Task", "Testing"],
required: [true, "Ticket type is required"]
},
ticketHistory: [{
type: mongoose.Schema.Types.ObjectId,
ref: "TicketHistory"
}],
ticketComment: [{
type: mongoose.Schema.Types.ObjectId,
ref: "TicketComment"
}],
createdOn: {
type: Date,
default: Date.now()
}
})
module.exports = mongoose.model("Ticket", TicketSchema)
Controller
exports.getSingleTicket = async (req, res) => {
try {
const ticket = await Ticket.findOne({ _id: req.params.id })
await ticket.populate("ticketHistory ticketComment").execPopulate()
res.status(200).json({
success: true,
// count: tickets.length,
data: ticket
})
} catch (error) {
console.error(error)
return res.status(500).json({ error: "Server Error" })
}
}