payload.find return no results when collection use 'localized: true' option

default discord avatar
megetron
7 months ago
3

I have just notice that when i create a string field 'slug' like this:



  fields: [
    {
      name: 'slug',
      type: 'text',
      unique: true,
      localized: true,
      index: true,
      admin: {
          readOnly: true,
          position: 'sidebar',
      },
      hooks: {
          beforeValidate: [
              ({ req: { payload }, data }) => {
                  if (payload) {
                      return slugify(data.name, {lower: true})
                  }
              },
          ],
      },
    },
    ...


with the localized:true option,




and execute a find query:


      let scrapped_brands = await payload.find({
        collection: "brands",
        where: {slug: { equals: 'slug1'}},
      });

the query returns no results even when there are actually results in the database.



after some investigation i have noticed that the buildQuery function doesnt build the query correctly.


on document 'node_modules/payload/dist/mongoose/buildQuery.js'



the query that was created is:

{ 'slug.null': { '$eq': 'apple' } }

when removing the

{localized: true}

option and build again, then i gets the documents as expected and the buildQuery shows a correct query like this:


{ slug: { '$eq': 'apple' } }


not sure if this is a bug, but i wasted few hours on this one 😦

  • discord user avatar
    jesschow
    Payload Team
    7 months ago

    Hey @megetron if you're querying on a localized collection, you need to specify the locale in your query. There are examples of how to do this with the local / rest API here

    https://payloadcms.com/docs/configuration/localization#retrieving-localized-docs
  • default discord avatar
    Jarrod
    7 months ago

    Also, did you enable localization project wide? You can find the info about that at the top of the docs page Jess sent over

  • default discord avatar
    megetron
    7 months ago

    didn't want to use the localization at all, so this issue was a mis-use of payload.



    maybe it was helpful to add a short warning message that says that the localize was not found instead of just retreive the 'slug.null' value.

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

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

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