import * as admin from "firebase-admin";
import DataModel from "../types/firebase";
export class FirebaseManager {
db = admin.database();
constructor() {
this.db = admin.database();
if (this.db === undefined) {
throw "cannot find database";
}
let data = this.db
.ref(`/`)
.get()
.then((snapshot) => {
if (!snapshot.exists()) {
throw "no database found :(";
} else {
let data = snapshot.val();
try {
let castedData = data as DataModel;
console.log("Database successfully initialised!");
} catch {
throw "Data could not be casted properly during initialisation";
}
}
});
}
}
Hi, I have been working on a discord bot that needs firebase as a backend. however, when i initialise this class, calling the .get() function says Error: Error: Client is offline
but I have been all the while online, anyone knows why? For reference, Im using the specific version "firebase-admin": "^9.11.1"
. If you would want to see all the code, https://github.com/MaxiGames/MaxiGames.js/tree/firebase
here it is.
Oops, the problem was that we needed to follow the tutorial here https://firebase.google.com/docs/admin/setup
and set a environment variable, then use admin.credential.applicationDefault()
as the credential parameter to the object that we input into he initialiseApp function. So, in the end the code should look like admin.initializeApp({credential:admin.credential.applicationDefault(), databaseURL:"https://{your-project}-default-rtdb.firebaseio.com"});