I'm learning React for a couple of months now and I struggle to populate some fields with mongoose I got a Projects and a User collection and each Project have a creator and a team of Users.
createdBy is a single ObjectId and participants is an array of objects containing the user.ObjectId and other fields related to the project.
Here's the clg of a project structure
{
_id: new ObjectId('65b518c381cfb1bec92fe0b9'),
title: 'Project 0',
createdBy: new ObjectId('65aabac39125371ade73ddf0'),
brief: 'Du blabla, plein de blabla',
deadline: 2024-03-20T18:00:00.245Z,
participants: [
{
_id: new ObjectId('65c241ed2d462330785bb2e4'),
accessLVL: 'lead',
joinDate: 2024-01-27T17:09:07.245Z,
userID: new ObjectId('65aabac39125371ade73ddf0')
},
{
_id: new ObjectId('65c241ed2d462330785bb2e5'),
accessLVL: 'member',
joinDate: 2024-01-27T17:09:07.245Z,
userID: new ObjectId('65aabac39125371ade73ddf4')
},
{
_id: new ObjectId('65c241ed2d462330785bb2e6'),
accessLVL: 'member',
joinDate: 2024-01-27T17:09:07.245Z,
userID: new ObjectId('65aabac39125371ade73ddf2')
}
],
submits: [
{
_id: new ObjectId('65c241ed2d462330785bb2e2'),
submitImage: [Array],
userId: new ObjectId('65aabac39125371ade73ddf4'),
update: 1999-12-31T23:00:00.000Z,
callback: [Array]
}
],
}
In my schemas, everything seems ok (Schema.Types.ObjectId and the ref to the user schema)
const ProjectSchema = new Schema(
{ title: String,
createdBy: {
type: mongoose.Schema.Types.ObjectId, ref: "User", require:true,
min: 2,
max: 100,
},
createdAt: {
type: Date,
required: true,
max: 50,
unique: true,
},
participants: [{
userID: {type: mongoose.Schema.Types.ObjectId, ref: "User", require:true},
accessLVL: {
type: String,
enum: ["member", "lead", "guest"],
default: "member",
},
joinDate: Date,
}],
deadline: Date,
brief: String,
submits: [{
userID: {type: mongoose.Schema.Types.ObjectId, ref: "User", require:true},
submitImage: [String],
update: Date,
callback: [{
userID: {type: mongoose.Schema.Types.ObjectId, ref: "User", require:true},
callback: String,
majDate: Date,
}],
}],
close: Boolean,
closeDate: Date,
},
{ timestamps: true }
);
const UserSchema = new Schema(
{id: {
type: String
},
avatar: {
type: String,
required: true,
max: 100,
},
name: {
type: String,
required: true,
min: 2,
max: 100,
},
email: {
type: String,
required: true,
max: 50,
unique: true,
lowercase: true,
index: true,
},
password: {
type: String,
required: true,
min: 5,
},
affiliate: {
type: String,
required: true,
min: 5,
},
contact: {
type:String,
max: 20,
},
ongoing: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Project",
}],
occupation: {
type: String,
max: 30,
},
status: {
type: String,
enum: ["team", "client"],
default: "team",
},
privilege: {
type: String,
enum: ["user", "manager", "admin"],
default: "user",
},
registerDate: Date,
lastSeen: Date,
},
{ timestamps: true }
);
But when I try to populate createdBy or participants.userID it return thoses fields as null
Here's the query
const fetchProjects = async (req, res) => {
try {
const projects = await Project.find().populate("createdBy");
console.log(projects[0]);
res.json({projects});
} catch(err) {
return res.sendStatus(404);
}
}
and the clg of the result
_id: new ObjectId('65b518c381cfb1bec92fe0b9'),
title: 'Project 0',
createdBy: null,
brief: 'Du blabla, plein de blabla',
...
I tried every populate method I found
Project.find().populate("createdBy")
and
Project.find().populate({path:"createdBy", model:"User"})
even with Project.find({}) like I saw somewhere but always the same createdBy: null, result. I'm surely messing something but I can't find what yet. Any advices ?