'mongoose subdocument instance name
Is there a way to get the model name of a subdocument ?
I have this code :
const BSchema = mongoose.Schema({
name: {
type: String
},
});
BSchema.post('save', async function () {
console.log(this.constructor.modelName, this.parent().constructor.modelName)
})
const BModel = mongoose.model('b', BSchema);
const ASchema = mongoose.Schema({
a: {
type: BModel.schema,
},
});
const AModel = mongoose.model('a', ASchema);
const aModel = new AModel({ a: { name: 'hello' } })
await aModel.save();
Expected output is : "b a" but I have "undefined a"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
