firebase

Firestore: GrpcConnection RPC 'Write' stream 0x48b21b62 error. Code: 13 Message: 13 INTERNAL: Received RST_STREAM with code 2


I get this error with the Firebase emulator when I try to insert a document. I'm a newbie in Firebase, so it might be I'm doing something stupid, but I can't figure it out.

Note: I took the code examples from the doc for the web version 9, as the code for node.js tells me there's no collection function in db. So I followed the advice in this thread

Here are my Firebase dependencies :

zied@zied-N752VX:~/Work/WS/linkedinCv-back$ pnpm view firebase dependencies
{
  '@firebase/app': '0.9.7',
  '@firebase/app-compat': '0.2.7',
  '@firebase/app-types': '0.9.0',
  '@firebase/auth': '0.22.0',
  '@firebase/auth-compat': '0.3.7',
  '@firebase/database': '0.14.4',
  '@firebase/database-compat': '0.3.4',
  '@firebase/firestore': '3.10.0',
  '@firebase/firestore-compat': '0.3.6',
  '@firebase/functions': '0.9.4',
  '@firebase/functions-compat': '0.3.4',
  '@firebase/installations': '0.6.4',
  '@firebase/installations-compat': '0.2.4',
  '@firebase/messaging': '0.12.4',
  '@firebase/messaging-compat': '0.2.4',
  '@firebase/storage': '0.11.2',
  '@firebase/storage-compat': '0.3.2',
  '@firebase/performance': '0.6.4',
  '@firebase/performance-compat': '0.2.4',
  '@firebase/remote-config': '0.4.4',
  '@firebase/remote-config-compat': '0.2.4',
  '@firebase/analytics': '0.9.5',
  '@firebase/analytics-compat': '0.2.5',
  '@firebase/app-check': '0.6.4',
  '@firebase/app-check-compat': '0.3.4',
  '@firebase/util': '1.9.3'
}

My initialization code is the following

import { initializeApp } from "firebase/app";
// import { getFirestore, connectFirestoreEmulator } from 'firebase-admin/firestore';

import { getFirestore, connectFirestoreEmulator } from "firebase/firestore";

// firebaseApps previously initialized using initializeApp()
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
    apiKey: "XXXXX",
    authDomain: "linkedincv-bdbec.firebaseapp.com",
    projectId: "linkedincv-bdbec",
    storageBucket: "linkedincv-bdbec.appspot.com",
    messagingSenderId: "472466845815",
    appId: "XXXXX",
    measurementId: "XXXXX",
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
// const db = getFirestore(app);

const db = getFirestore(app);
connectFirestoreEmulator(db, 'localhost', 4000);

export {db}

The code that triggers the error is the following:

import {db} from '../../../lib/middleware/firebaseInit'
import { doc, setDoc } from "firebase/firestore";

export async function POST({request}) {
    try {
        const payload = await request.json();
        const uniqueUrl = payload.uniqueUrl;

        // Store the 'uniqueUrl' field in Firestore
        const docRef = await setDoc(doc(db, "json_cv", "testId"), payload)
        // Return the document ID of the stored object
        return {
            body: JSON.stringify({ documentId: docRef.id }),
            headers: { 'Content-Type': 'application/json' },
            status: 200
        };
    } catch (error) {
        // Handle any errors that occur during processing
        console.error(error);
        return {
            body: JSON.stringify({ error: 'Failed to store unique URL' }),
            headers: { 'Content-Type': 'application/json' },
            status: 500
        };
    }
}

I saw some people updated firebase-functions to get it to work, but it seems this doesn't apply for me as I'm already at version 9.

Here's the error I receive:

[2023-04-13T06:09:01.286Z]  @firebase/firestore: Firestore (9.19.1): GrpcConnection RPC 'Write' stream 0x48b21b62 error. Code: 13 Message: 13 INTERNAL: Received RST_STREAM with code 2 triggered by internal client error: Protocol error 

Solution

  • An answer to this is in the other question I asked

    Long story short: when running in node.js, we have to use the library from "firebase-admin" and not from "firebase"

    Also, the emulator is not initialized, only an environment variable has to be set before initializing firebase.