Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Firestore addDoc throws on a field whose value is undefined

ErrorFirebaseError: Function addDoc() called with invalid data. Unsupported field value

Short answer

Firestore has no undefined type. When an object you pass to addDoc or setDoc has a field set to undefined, the SDK refuses the write. Replace that value with null, leave the field out, or turn on the ignoreUndefinedProperties setting when you initialize Firestore so such fields are skipped.

  • Applies to Firebase JS SDK 12 and 13, Firestore, React
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log the object right before the write and look for a key whose value is undefined. Plain console.log hides it in some tools, so print the keys with their values.

    console.log(Object.entries(data).filter(([, v]) => v === undefined));
  2. 2Match what you see, then jump to its fix

    1. 1The filter above prints at least one keyA form field or API value is undefinedFix
    2. 2Many objects, often built from large forms or models, and undefined fields are expected and harmlessYou want the SDK to skip undefined values for youFix

What does the error look like?

  • FirebaseError: Function addDoc() called with invalid data. Unsupported field value

Cause 1 of 2

A form field or API value is undefined

1Check

The usual sources are an input the user left empty, a prop that was never passed, or a lookup that found nothing. Firestore supports null as a value but its data type list has no undefined.

console.log(Object.entries(data).filter(([, v]) => v === undefined));

2Fix

Decide what the field should be: null if you want an empty field stored, or no field at all. Build the object so it only contains keys that have a value.

const data = {
  title,
  note: note ?? null,          // stored as null
  ...(dueDate && { dueDate }), // key is left out when empty
};
await addDoc(collection(db, 'tasks'), data);

3Verify

The write succeeds and the new document appears in the Firebase console.

console.log('saved', (await addDoc(collection(db, 'tasks'), data)).id);

Confirmed by firebase.google.com

Did this fix it?

Cause 2 of 2

You want the SDK to skip undefined values for you

1Check

Firestore settings include ignoreUndefinedProperties. It is off by default, so the SDK throws on undefined properties.

grep -rn "getFirestore\|initializeFirestore" src

2Fix

Create the Firestore instance with initializeFirestore and the setting turned on. It has to run before any other Firestore call, including getFirestore().

import { initializeApp } from 'firebase/app';
import { initializeFirestore } from 'firebase/firestore';

const app = initializeApp(firebaseConfig);
export const db = initializeFirestore(app, {
  ignoreUndefinedProperties: true,
});

3Verify

Repeat the write that failed. Fields set to undefined are no longer written, and the call does not throw.

await addDoc(collection(db, 'tasks'), { title: 'test', note: undefined });

Confirmed by firebase.google.com

Did this fix it?

How do I stop it coming back?

  • Normalize form data in one function before writing, so every field is either a real value or null.
  • Decide once per project whether empty means null or missing, and keep that rule everywhere.
  • Call initializeFirestore in a single module that every file imports, so the setting cannot be skipped.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.