.populate() and .toJSON() override #11899
Unanswered
tamis-laan
asked this question in
Q&A
Replies: 2 comments 1 reply
-
@tamis-laan can you please provide a script that demonstrates the behavior you're seeing? |
Beta Was this translation helpful? Give feedback.
0 replies
-
const ASchema = Schema({
b = {type: ObjectID, ref:'B'}
})
const Bschema = Schema({
name: String
})
BSchema.method('toJSON', function() {
let b = this.toObject()
b.message = `Hello ${b.name}`
return b
})
const a = await A.findById(someid).populate('b')
const json = a.toJSON() The json doesn't include |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
So I have two schema's A and B where A contains a reference to a document of schema B. Both schema's have there
.toJSON
methods overridden in order to transform and remove certain properties when send to the client. This works for documents with schema A or B in isolation. However when populating a document with schema B where a property refers to a document with schema A, the.toJSON
logic for the property with schema A is not applied.How should I fix this?
Beta Was this translation helpful? Give feedback.
All reactions