Search code examples
node.jsfirebasefirebase-realtime-databasefirebase-admin

when i use firebase.database().goOnline(); I get an error


this is my code

admin.initializeApp({...});

var db = admin.database();
let ref = db.ref(...);

await ref.once("value",function () {...},);

firebase.database().goOffline();
firebase.database().goOnline();

 ref.on("value",function () {...},);
);

when i use firebase.database().goOnline(); I get an error

Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).
    at app

Solution

  • You're mixing two ways of addressing Firebase.

    You can access Firebase through:

    1. Its client-side JavaScript SDK, in which case the entry point is usually called firebase.
    2. Its server-side Node.js SDK, in which case the entry point is usually called admin.

    Now you initialize admin, but then try to access firebase.database(). And since you did call admin.initializeApp that's where the error comes from.

    My guess is that you're looking for admin.database().goOnline() or db.goOnline().

    Also note that there is no reason to toggle goOffline()/goOnline() in the way you do now, and you're typically better off letting Firebase manage that on its own.