Open DanishFayaz opened 6 years ago
Please provide some example of your schema and expected results.
For example:
paymentMethods: [{
type: {
type: String,
enum: PaymentTypes
},
description: {
type: String,
intl: true
}
}]
I can confirm this behavior as well. Any update here?
Hey. Did you find any fix? I have the same problem.
What I found facing the same issue, is that this.ownerDocument
is always undefined
in the schema.virtual(path).get(...)
function.
Workaround is for sure explicitly call setLanguage(...)
function on each populated document.
My schema:
const testSchema = new Schema({
name: { type: String, intl: true },
subdocuments: [{
description: { type: String, intl: true }
}]
}, {
toJSON: {
virtuals: true
}
})
testSchema.path('subdocuments').schema.options.toJSON = { virtuals: true }
testSchema.plugin(mongooseIntl, { languages: ['en', 'de'], defaultLanguage: 'en' });
And the acquire function:
const data = await Test.findById(req.params.id).exec()
data.setLanguage(language)
res.send(data)
Would be great to have this issue fixed somehow... Thanks!
Update
After some further research, I found the following issue in the mongoose repository: https://github.com/Automattic/mongoose/issues/8092#issuecomment-524630095. I guess that's why populated documents don't apply parent's language.
When I populate a document containing sub documents from different models. Even though those models have the intl fields applied. The populate result only applies translation on the Parent model only. Subdocuments show all fields instead of particular lang one.
I've tried all methods, document level setLanguage, schema level setDefaultLanguage etc.
Also I've also added hooks on sub documents of pre and post find and called the setLanguage there as well. But the populate call from parent response only gets me back the updated lang string of parent document.