Please consider the following parentSchema and childSchema:
const parentSchema = new mongoose.Schema({
name: String,
children: [{ type: mongoose.Schema.Types.ObjectId, ref: "Child" }],
});
const childSchema = new mongoose.Schema({
name: String,
parent: { type: mongoose.Schema.Types.ObjectId, ref: "Parent" },
});
How do I access the name of the parent within a post middleware of the childSchema?
I am trying the code below but parent is assigned the ObjectId instead of the actual parent model.
childSchema.post("save", async function (child) {
const parent = child.populate("parent").parent;
});
What's the right way to do this?