delete data on field hook

default discord avatar
reepicheep05last year
2

Hey all, I'm trying to add a hook to a text field that takes in a

location

string and uses it to fetch a geoJson object which is then stored in a hidden field called

geoJson

. This part is working great, but I am having trouble deleting the geoJson object when the field is cleared.



In the hook, I am taking in a few arguments

{ operation, value, originalDoc, data }

and I can manipulate the

data

object, but when I try to set the

data.geoJson

field to

null

or

undefined

or even just delete

data.geoJson

the submission works but the data remains in place. If I instead update the field to say an empty object, that will work. I'm just not seeing how to just remove it completely.



I believe I could use the localAPI to update the field, but I'm thinking that there is probably a way to do so in the data object from the hook.

  • discord user avatar
    jesschow
    10 months ago

    Hi @reepicheep05 sorry for the delay here!



    Currently the appropriate way to clear your

    data.geoJson

    field

    is

    to update it to an empty object.



    The idea of clearing / unsetting a field has come up a few times and we plan to add this functionality to our upcoming roadmap.



    Someone else raised this over on GitHub, you can keep in the loop here:

    https://github.com/payloadcms/payload/discussions/3102
  • default discord avatar
    reepicheep0510 months ago

    Ok, thanks for the reply!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

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