Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

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

default discord avatar
megetron_2 years 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 😦

  • default discord avatar
    jessrynkar2 years ago

    Hey

    @1085675413130465291

    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
  • discord user avatar
    jarrod_not_jared
    2 years 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_2 years 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.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.