You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Error: Value for argument "data" is not a valid Firestore document. Couldn't serialize object of type "Bytes" (found in field "binaryData"). Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the "new" operator).
Serializing works when i send it as Uint8Array but then it's displayed as a base64 string in firestore emulator..?
And when i try to retrieve it with Web SDK it return to me as byteString/binaryString object..?
What's the correct way to store bytes in firestore?
The correct type depends on which SDK you're using. Here's the full picture:
Admin SDK (Node.js) — use Buffer
import{getFirestore}from'firebase-admin/firestore';constdb=getFirestore();// Storingconstbuffer=Buffer.from('hello world','utf-8');// From a file: const buffer = fs.readFileSync('./image.png');awaitdb.collection('files').doc('doc-id').set({data: buffer,// Buffer is accepted directly by the Admin SDK});// Retrieving — comes back as Buffer on Node.jsconstsnap=awaitdb.collection('files').doc('doc-id').get();constretrieved: Buffer=snap.data()!.data;console.log(retrieved.toString('utf-8'));// 'hello world'
The correct type depends on which SDK you're using. Here's the full picture:
Admin SDK (Node.js) — use Buffer
import{getFirestore}from'firebase-admin/firestore';constdb=getFirestore();// Storingconstbuffer=Buffer.from('hello world','utf-8');// From a file: const buffer = fs.readFileSync('./image.png');awaitdb.collection('files').doc('doc-id').set({data: buffer,// Buffer is accepted directly by the Admin SDK});// Retrieving — comes back as Buffer on Node.jsconstsnap=awaitdb.collection('files').doc('doc-id').get();constretrieved: Buffer=snap.data()!.data;console.log(retrieved.toString('utf-8'));// 'hello world'
Web SDK (browser) — use Bytes
import{getFirestore,doc,setDoc,getDoc,Bytes}from'firebase/firestore';constdb=getFirestore();// Storing — wrap your Uint8Array in Bytesconstuint8=newTextEncoder().encode('hello world');awaitsetDoc(doc(db,'files','doc-id'),{data: Bytes.fromUint8Array(uint8),// or from base64: Bytes.fromBase64String('aGVsbG8gd29ybGQ=')});// Retrievingconstsnap=awaitgetDoc(doc(db,'files','doc-id'));constbytes: Bytes=snap.data()!.data;constdecoded=newTextDecoder().decode(bytes.toUint8Array());console.log(decoded);// 'hello world'
Why Blob and plain objects fail
Firestore only serializes its own special types plus native JS primitives. Blob has a custom prototype and is not on that list — hence the "Firestore doesn't support JavaScript objects with custom prototypes" error. Uint8Array works because the SDK internally converts it to the Firestore Bytes type.
About the emulator showing base64
That's expected. The Firestore emulator UI displays Bytes fields as base64 strings — it's a display choice, not the actual stored format. When you retrieve the document in code, you get Buffer (Admin SDK) or Bytes (Web SDK), not a string.
TL;DR:Buffer with Admin SDK, Bytes.fromUint8Array() with Web SDK.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
The correct type depends on which SDK you're using. Here's the full picture:
Admin SDK (Node.js) — use
BufferWeb SDK (browser) — use
Bytes