Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Clear/delete field using Local API

default discord avatar
allannnc2 years 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
    2 years 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.

  • default discord avatar
    jessrynkarlast year

    Hey

    @946299884070375424

    did this get resolved?

  • default discord avatar
    allannnclast year

    Hey

    @854377910689202256

    not afaik



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

  • default discord avatar
    jessrynkarlast year
    @946299884070375424

    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
    allannnclast year

    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

  • default discord avatar
    jessrynkarlast year
    @946299884070375424

    bug report please 👍

  • default discord avatar
    jessrynkarlast year
    @946299884070375424

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

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.