Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DanishFayaz opened this issue Jun 11, 2018 · 5 comments
Open

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

DanishFayaz opened this issue Jun 11, 2018 · 5 comments

Comments

@DanishFayaz
Copy link

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
Copy link
Owner

alexsk commented Jul 7, 2018

Please provide some example of your schema and expected results.

@alkanyunus
Copy link

alkanyunus commented Aug 5, 2018

For example:

paymentMethods: [{
        type: {
            type: String, 
            enum: PaymentTypes
        },
        description: {
            type: String,
            intl: true
        }
    }]

@jmikrut
Copy link

jmikrut commented May 15, 2019

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

@itsmrTech
Copy link

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

@whydrae
Copy link

whydrae commented May 15, 2020

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: Automattic/mongoose#8092 (comment). I guess that's why populated documents don't apply parent's language.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants