#delete data on field hook

2 messages · Page 1 of 1 (latest)

fresh steeple
#

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.

blissful fossil
#

Hi @fresh steeple 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

GitHub

Link to reproduction ? To Reproduce Passing undefined Attempt to clear a field like so: The code remains the same within the DB (here I'm assuming Payload doesn't know that the data.code ke...