firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

Firebase Database (${JSCORE_VERSION}) INTERNAL ASSERT FAILED: Unknown node type

Open ljrahn opened this issue 2 years ago • 4 comments

Operating System

Mac M1

Browser Version

Chrome/121.0.6167.139 (Official Build) (arm64)

Firebase SDK Version

^10.8.0

Firebase SDK Product:

Database

Describe your project's tooling

NextJS default bundler

Describe the problem

When firebase initialization logic is seperated into a seperate package, and imported, an error occurs with realtime database listeners. This issue as far as i know only occurs with the realtime database listeners ie. onChildAdded, onValue, and doesnt occur with eg. get, or any other service, eg. firestore. Additionally, it only occurs when seperated, built, and imported. If i define the initilization logic directly in the nextjs app, it works as expected. Furthermore, this only occurs when I have 2 conditions on the query, eg. limitToFirst(5) AND orderByChild("updatedAt"), if only one is present, the error is not triggered (although there is another weird issue if only limitToFirst is set, then onChildRemoved is triggered immediately after onChildAdded, even though it shouldnt be, but regardless, this is less important).

Note I have been using this strategy of building the initilization logic in a seperate package and importing it for a while. This only started occuring when I try to do what is described: toggle a realtime database listener with two QueryConstraint's

Steps and code to reproduce issue

https://github.com/ljrahn/firebase-unknown-node-type

  1. Clone the repo: [email protected]:ljrahn/firebase-unknown-node-type.git
  2. npm install
  3. npm run build:functions
  4. npm run build:deps
  5. npm run dev
  6. Open http://localhost:3000.
  7. Click Add Data!
  8. Error Error: Firebase Database (${JSCORE_VERSION}) INTERNAL ASSERT FAILED: Unknown node type should occur.
  9. Experience pain...

Note in ./src/pages/index.tsx we are importing import { initializeFirebase } from "../../deps/build/firebase". if this is changed to import { initializeFirebase } from "../../deps/firebase", the error disapeers, and we fetch the data as expected.

ljrahn avatar Feb 06 '24 22:02 ljrahn

facing same issue when used in Vue3 project. Here in my case i am having multiple listener on the same path, eg: childChanged, childRemoved. It works when one of the listener is commented.

suhail23599 avatar May 15 '24 10:05 suhail23599

Thanks for the repro. Just a quick note before fully looking into it - adding a couple of missing steps that are needed to fully repro:

Steps and code to reproduce issue

https://github.com/ljrahn/firebase-unknown-node-type

  1. Clone the repo: [email protected]:ljrahn/firebase-unknown-node-type.git
  2. npm install

also cd functions and npm install there

  1. npm run build:functions
  2. npm run build:deps

Start the emulator with npm run emulator - this requires that you have the Firebase CLI installed (npm install -g firebase-tools if not)

  1. npm run dev
  2. Open http://localhost:3000.
  3. Click Add Data!
  4. Error Error: Firebase Database (${JSCORE_VERSION}) INTERNAL ASSERT FAILED: Unknown node type should occur.
  5. Experience pain...

hsubox76 avatar May 15 '24 17:05 hsubox76

The problem is that you are importing 2 separate bundles of firebase/database. You have imports from firebase/database in both index.tsx and in deps/build/firebase.js (generated file). Unfortunately, when you build deps/build/firebase.js, you have the tsconfig "module" set to "NodeNext", so it outputs a commonjs file with require statements. This causes it to import the CJS bundle of @firebase/database. Meanwhile, your index.tsx is a standard ESM file with import statements, so it's importing the ESM bundle of @firebase/database. This means there's two copies of the code, and the corresponding internal classes, so when it does an "instanceof" check for some data, it fails when it should succeed, because there's 2 copies of the class and it's checking the wrong copy.

It works when you import from "deps/firebase" instead because now you're using the TS ESM source file with "import" statements, so it brings in the same bundle of firebase/database as index.tsx does.

A quick fix is to change your deps/tsconfig.json file from "module: NodeNext" to "module: ES2020". I did this and it fixed the bug. Another option is to not build deps separately and import from deps/firebase.ts directly. It's not clear to me why that extra build step is necessary but I don't know the context of your project architecture so that's up to you. In any case, if you do want to build it, make sure the built artifact is in the same format (ESM) as your main project.

facing same issue when used in Vue3 project. Here in my case i am having multiple listener on the same path, eg: childChanged, childRemoved. It works when one of the listener is commented.

I would also check if you are importing firebase/database in multiple parts of your project that are treated differently (some are require, some are import, some are built separately, etc)

It seems like this isn't a bug in the Firebase SDK but I would like verification that this fixes the issue for everyone, so I'll leave it open with the needs-info tag where the bot will close it in 10 working days if no one replies.

hsubox76 avatar May 15 '24 17:05 hsubox76

Hi @hsubox76 , we have a micro-frontend application, where ,in both the services, we import firebase/database. is this an issue? We are trying to use the firebase DB reference from main-ui, and use the same DB refrence it in our service, so that i don't have to re-authenticate again in our service. However sdk methods are imported separately in both the service These 2 services are built separately. any workaround you would like to suggest?

suhail23599 avatar May 16 '24 06:05 suhail23599

Hi @suhail23599,

If I understand correctly, this might be the use case for FirebaseServerApp. It can be initialized with an Auth Token created from a user session on a client. You will need to send the Auth token to your service, where It will to sign-in the user in your application's backend to perform user-context (ie: not admin context) operations, such as the use of RTDB. We've added it to the SDK to support SSR Frameworks, which have both client and server-side rendering phases.

DellaBitta avatar May 22 '24 14:05 DellaBitta

Hey @ljrahn. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar May 29 '24 01:05 google-oss-bot

Since there haven't been any recent updates here, I am going to close this issue.

@ljrahn if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.

google-oss-bot avatar Jun 05 '24 01:06 google-oss-bot

We resolved this issue by importing firebase SDK methods only in one service, ie. in our case it was a parent ui, then we exposed all these methods to child UI. For anyone who is having this issue, can try this approach, Good luck!

suhail23599 avatar Jun 18 '24 08:06 suhail23599