lastModifiedBy

default discord avatar
bhavikak
8 months ago
12

i'm checked in users api it's storing multiple objects of lastModifiedBy:{} like shown in image, i want to know the reason

  • discord user avatar
    jesschow
    Payload Team
    8 months ago

    oh no that is weird - do you mind sharing your user collection?

  • default discord avatar
    bhavikak
    8 months ago

    @jesschow why not!

  • default discord avatar
    Jarrod
    8 months ago

    Are you adding lastModifiedBy with a plugin or similar?

  • default discord avatar
    bhavikak
    8 months ago
  • default discord avatar
    Jarrod
    8 months ago

    So what I see is what I would expect, I think. The shape you are seeing is not what is stored in the db, the db just stores the value as id and the relationTo. If you set a

    depth

    parameter in your query params

    ?depth=1

    what do you get?



    Think about it if the last modified was another user, it would fulfill that user and they would have a last modified field, that would get fulfilled and so on. The only thing here is, it’s a circular reference so you need a way to break the chain. I would likely create another field

    lastModifiedBySelf

    and then check the user id on the request with the id on

    originalDoc

    , if they match set the new checkbox and clear the relationship, else set the relationship.



    See all arguments for beforeChange hook here

    https://payloadcms.com/docs/hooks/collections#beforechange


    OR, you could set

    maxDepth

    on the relationship field, probably more-so what you are looking for.

    https://payloadcms.com/docs/fields/relationship#config
  • default discord avatar
    bhavikak
    8 months ago

    okay so what i have to do now?

  • default discord avatar
    Jarrod
    8 months ago

    Have you tried either of the things I mentioned above?

  • default discord avatar
    bhavikak
    5 months ago

    hiii @Jarrod i tried it. if i passed depth=1 i got "lastModifiedBy": {


    "value": {


    "id": "633aa17cd6c8ab006231e6b3",


    "email": "------------",


    "createdAt": "2022-10-03T08:46:52.557Z",


    "updatedAt": "2023-03-29T10:10:27.673Z",


    "enableAPIKey": false,


    "lastModifiedBy": {


    "value": "633aa17cd6c8ab006231e6b3",


    "relationTo": "users"


    },


    "name": "Bhavika ",


    "roles": [


    "admin"


    ]


    },


    "relationTo": "users"


    }, and i save page by another user and i set depth=60 it's fullfill the user with adding new object.



    so on a each save it's adding new object

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.