Custom query not filtering data

default discord avatar
aaronksaunders
last week
2
{
      path: "/me",
      method: "get",
      handler: async (req, res, next) => {

        if (!req?.user?.id) {
          return res.status(404).send({ error: "user not found" });
        }

        console.log(req.user.id)

        const query = {
          "owner.id":   req.user.id, 
        };

        const data = await payload.find({
          depth : 2,
          collection: "thoughts",
          where : {
            "owner.id":   req.user.id, 
          }
        });

        
        if (data) {
          console.log(data)
          res.status(200).send( data );
        } else {
          res.status(404).send({ error: "not found" });
        }
      },
    },
  • discord user avatar
    jarrod_not_jared
    Payload Team
    last week

    Hey @aaronksaunders! Try adjusting your query to the following:



    const data = await payload.find({
      depth : 2,
      collection: "thoughts",
      where : {
        owner: {
          equals: req.user.id
        }
      }
    });


    Here is the section in the docs that talks about querying:

    https://payloadcms.com/docs/queries/overview
  • default discord avatar
    aaronksaunders
    last week

    Thanks, I figured it out late last night after trial and error, and I did read this documentation but there is no specific example on relationships. I was checking against owner.id and not owner 🙏🏾



    I like what I see with payload, a little worried about running into things like this when working on a client project… trying to come up with all with a robust example project to get all of the questions answered so we are not blocked when we are on the clock. Thanks for your help @jarrod_not_jared

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.