Clear/delete field using Local API

default discord avatar
allannnc
4 months ago
8

Hi, I'm running into an issue when trying to clear a specific field using

payload.update

, my call looks like


const updatedDoc = await req.payload.update({
  collectionName: 'foo',
  id: 'someId',
  data: {
    nonRequiredStringFieldToClear: undefined
  }
});


I'm assuming the Local API doesn't know that

nonRequiredStringFieldToClear

is even being specified since its value is

undefined

. Also unable to pass

null

as the generated type for the field is

string | undefined

.



A workaround so far is to set

nonRequiredStringFieldToClear: ""

but actually clearing the field would be nicer.



Has anyone else run into this? Thanks!



Noting we could also use the mongoose model with

$unset

, but, for now, just going to set to an empty string here for simplicity

  • discord user avatar
    jarrod_not_jared
    Payload Team
    4 months ago

    Interesting, yeah I think null would be the preferred value to save. Undefined will not work because we merge existing data with incoming data.



    But like you said the type is not allowing null, which seems like it should.

  • discord user avatar
    jesschow
    Payload Team
    2 months ago

    Hey @allannnc did this get resolved?

  • default discord avatar
    allannnc
    2 months ago

    Hey @jesschow not afaik



    Stuck with the workaround of passing "empty" non-null values

  • discord user avatar
    jesschow
    Payload Team
    2 months ago

    @allannnc hmm not ideal... could you throw this information into an issue over on GitHub for us?

    https://github.com/payloadcms/payload/issues
  • default discord avatar
    allannnc
    2 months ago

    will reproduce and create this issue if needed!



    would this be a bug report or feature request?



    for reference: this does not clear the

    code

    field, acts as if

    code

    was never passed in

    data


    and this is not allowed:



    so, this is the workaround

    image.png
    image.png
    image.png
  • discord user avatar
    jesschow
    Payload Team
    2 months ago

    @allannnc bug report please 👍

  • discord user avatar
    jesschow
    Payload Team
    2 months ago

    @allannnc awesome thank you, we'll get this resolved soon 👍

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.