Firestore addDoc throws on a field whose value is undefined
FirebaseError: Function addDoc() called with invalid data. Unsupported field valueShort 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.
Fix it fast
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));2Match what you see, then jump to its fix
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
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" src2Fix
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Web apps
Attempted import error: firebase/app has no default export
Attempted import error: 'firebase/app' does not contain a default export (imported as 'firebase').
Since the modular SDK (version 9), firebase/app no longer exports one firebase object.
3 causes, each with a fixWeb apps
Firebase Messaging cannot register its default service worker
messaging/failed-service-worker-registration
Firebase Messaging registers a file called firebase-messaging-sw.js from the root of your domain.
3 causes, each with a fixWeb apps
A Vite single page app shows 404 when you refresh a route on the live site
Page not found (Netlify) on a route that works inside the app
Client side routes like /dashboard are not real files in dist.
3 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fix