Custom endpoint sends back localized fields

default discord avatar
3 months ago

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()
              order: -1,
              lastNameWithoutPrefix: 1,
            .limit(parseInt(query.limit as string))
              (parseInt( 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?

  • default discord avatar
    3 months ago

    @NH02 what if you specify the


    property on the find request

    Does that then deliver the string value?

  • discord user avatar
    Payload Team
    3 months ago

    so, because you're using the Mongoose model directly (and not the Payload


    operation) 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


    field 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


    is set on the


    and then the


    hook is what looks at that, and then flattens the locales into the locale you ask for via the query param

  • default discord avatar
    3 months ago

    Alright it works! Thank you 🙆‍♂️ @jmikrut

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!


Connect with the Payload Community on Discord



Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.