使用Firebase Admin SDK更新文档时遇data参数无效错误求助
Hey there, let's tackle this error you're seeing when using Firebase Admin SDK to update documents. That message is Firebase's way of saying: "The data you're trying to save isn't a plain JavaScript object—and that's what I need to work with."
Common Causes & Fixes
Let's break down the most likely reasons you're hitting this, along with concrete fixes for your TypeScript code.
1. You're passing a class instance instead of a plain object
TypeScript classes are great for type safety, but instances of classes have a prototype chain attached to them. Firebase doesn't recognize these as valid document data because they aren't "plain" objects.
Example of the problem:
class User { name: string; constructor(name: string) { this.name = name; } } const user = new User("Miguel"); // This will throw the error! await updateDoc(doc(db, "users", "123"), user);
How to fix it:
Convert the class instance to a plain object. You have a few options:
- Use the spread operator (
...) to create a new plain object with the instance's properties:await updateDoc(doc(db, "users", "123"), { ...user }); - Add a
toJSON()method to your class (Firebase will use this automatically to serialize the instance):class User { name: string; constructor(name: string) { this.name = name; } toJSON() { return { name: this.name }; } } const user = new User("Miguel"); // Now this works! await updateDoc(doc(db, "users", "123"), user);
2. Your data contains non-serializable values
Firebase only accepts plain objects with serializable values—things like strings, numbers, booleans, arrays, or other plain objects. If your data includes functions, Symbol types, or other non-serializable properties, you'll get this error.
Example of the problem:
const userData = { name: "Miguel", getFullName: () => "Miguel Slv" // Functions are not allowed! }; await updateDoc(doc(db, "users", "123"), userData);
How to fix it:
Remove or filter out the non-serializable properties. You can do this manually, or use a utility function to clean up the object:
- Manual cleanup (best for simple objects):
const plainUserData = { name: userData.name }; await updateDoc(doc(db, "users", "123"), plainUserData); - Utility function for more complex objects:
function sanitizeFirebaseData(obj: Record<string, any>): Record<string, any> { return Object.fromEntries( Object.entries(obj).filter(([_, value]) => { const type = typeof value; return type !== 'function' && type !== 'symbol' && value !== undefined; }) ); } await updateDoc(doc(db, "users", "123"), sanitizeFirebaseData(userData));
3. You're using a non-plain object from a library
If you're using objects from libraries like Immutable.js, or other data structures that wrap plain objects, Firebase won't recognize them as valid. For example, an Immutable Map isn't a plain JS object.
How to fix it:
Convert the library-specific object to a plain JS object. For Immutable.js, that means using toJS():
import { Map } from 'immutable'; const userMap = Map({ name: "Miguel" }); await updateDoc(doc(db, "users", "123"), userMap.toJS()); // Converts to plain object
Quick Recap
Firebase Admin SDK expects document data to be a plain JavaScript object—no prototype chains, no non-serializable values, no library-specific data structures. By converting your data to fit this requirement, you'll resolve the error.
内容的提问来源于stack exchange,提问作者MiguelSlv

