1

My Firestore document has a field with a value already set.

Now I want to remove the value by setting it to something that signifies "empty" such as null or undefined.

So far I have tried doing this with the updateDoc method like so:

updateDoc(documentReference,{ photoURL: undefined })

But it throws this error:

Function updateDoc() called with invalid data. Unsupported field value: undefined (found in field photoURL in document users/d23xxxxxxxxxxxxxxx12)

And setting it to null is also not allowed because if I try to set it to null like so:

updateDoc(documentReference,{ photoURL: null })

TypeScript will throw this error:

Type 'null' is not assignable to type 'FieldValue | Partial | undefined'.

I found that using an empty string like updateDoc(documentReference,{ photoURL: "" }) works but it's not as elegant as null or undefined.

I will use the empty string if no other solutions are found, but perhaps there is another way?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
TinyTiger
  • 1,801
  • 7
  • 47
  • 92
  • You could store that as `null` itself just in case you need to query documents where a field is missing or is empty. – Dharmaraj Apr 26 '22 at 03:55
  • @Dharmaraj I can't set it to `null` explictly because that also throws an error. Sorry that wasn't clear, I updated the question to reflect that. – TinyTiger Apr 26 '22 at 03:57

1 Answers1

3

Firestore does not recognize javascript undefined as a valid value. You can see the valid types in the documentation. Null is a valid type, so you should be able to use that. If you are not, then you should file a bug against the SDK that you're using. If you're using the web client SDK, use this GitHub. Be sure to provide complete steps to reproduce.

If you want to simply remove the field altogether so that it has no type at all, just remove the field and check for the the missing field in the code the performs the query.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • If `null` is allowed then why does TypeScript throw a type error? It isn't typed to allow `null`. P.S. I want to keep the field but give it a `null` value, not remove the field altogether. – TinyTiger Apr 26 '22 at 04:05
  • 1
    File a bug as described in my edit. You will need to provide a fully reproducible example that anyone can use to see the behavior. – Doug Stevenson Apr 26 '22 at 04:06
  • Ok will do that now. – TinyTiger Apr 26 '22 at 04:07
  • 1
    Oops! The TS `null` error was caused on my part, not by Firestore. `null` values are indeed accepted. There is no bug. – TinyTiger Apr 26 '22 at 04:15