alexsk / mongoose-intl

Mongoose schema plugin for multilingual fields
MIT License
74 stars 31 forks source link

Subdocuments on populate don't get the lang feature applied #14

Open DanishFayaz opened 6 years ago

DanishFayaz commented 6 years ago

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.

alexsk commented 6 years ago

Please provide some example of your schema and expected results.

alkanyunus commented 6 years ago

For example:

paymentMethods: [{
        type: {
            type: String, 
            enum: PaymentTypes
        },
        description: {
            type: String,
            intl: true
        }
    }]
jmikrut commented 5 years ago

I can confirm this behavior as well. Any update here?

itsmrTech commented 5 years ago

Hey. Did you find any fix? I have the same problem.

whydrae commented 4 years ago

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.