I'm working with a custom endpoint for one of my collections:
...
endpoint: [{
...
handler: async (req, res) => {
const { payload, query } = req;
const teamMembers = {
docs: await payload.collections['team-members'].Model.find()
.sort({
order: -1,
lastNameWithoutPrefix: 1,
})
.limit(parseInt(query.limit as string))
.skip(
(parseInt(query.page as string) - 1) *
parseInt(query.limit as string),
),
};
return res.status(200).send(teamMembers);
},
...
}]It sends all items from my collection back, however what I noticed is that the data for localized fields comes back as:
localizedField: {nl: 'string value'}Normally payload sends the string back as
localizedField: 'string value'. Is there a way to tell the custom endpoint to do this as well?
what if you specify the
localeproperty on the find request
Does that then deliver the string value?
so, because you're using the Mongoose model directly (and not the Payload
findoperation) Mongoose is just sending back the raw document itself (including all locales). Payload's operations are responsible for the localization "flattening", not Mongoose. If I were you, I'd try to leverage the Payload local api instead of the raw Mongoose model. But if you can't do that, you could always run the Payload
afterReadfield hooks manually against the documents that are returned from Mongoose
here is where we "flatten" locales within the Payload core itself:
notice how we pass in the raw Mongoose docs?
once they come back out, they will have locales "flattened". The
localeis set on the
reqand then the
afterReadhook is what looks at that, and then flattens the locales into the locale you ask for via the query param
?locale=myLocaleAlright it works! Thank you 🙆‍♂️
@364124941832159242Star
Discord
online
Get dedicated engineering support directly from the Payload team.