Search code examples
javascriptfirebasegoogle-cloud-firestoreblob

Create document with byte array: Cannot read properties of undefined (reading 'fromUint8Array'


I'm trying to set a byte array as a Firestore document field.

Based on suggestions elsewhere, it should be as simple as using fromUint8Array(), as below:

const bytes = [72, 73];
const test = firestore.Blob.fromUint8Array(
   new Uint8Array(bytes)
);

But I get the error

TypeError: Cannot read properties of undefined (reading 'fromUint8Array')

I am completely new to Firebase/Firestore and I don't know where I'm going wrong, so any pointers would be much appreciated.

My set-up:

  • firebase: "^9.9.2"
  • import { getFirestore, serverTimestamp, collection, query, where, runTransaction, orderBy, limit, getDocs, doc, setDoc, connectFirestoreEmulator } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-firestore.js";
  • using the webhost, firestore emulators: connectFirestoreEmulator(db, 'localhost', 8080);

Solution

  • The Blob class is not available in the modular SDK. But Bytes is: https://firebase.google.com/docs/reference/js/firestore_.bytes

    Swap out firestore.Blob for Bytes, and include it in your import line and it works

    import { getFirestore, connectFirestoreEmulator, Bytes } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-firestore.js";
    
    const bytes = [72, 73];
    const test = Bytes.fromUint8Array(
       new Uint8Array(bytes)
    );