admin管理员组

文章数量:1314442

I am trying to access my realtime database in firebase but it shows me this error Uncaught Error: Service database is not available. I have searched for what this could posabbly mean but I couldn't find anything useful or a solution.

Here is my code:

window.addPerson = addPerson;

const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const userVar = urlParams.get('user')

const userVarSplitted = userVar.split('#')
const userVarFormatted = userVarSplitted[0] + ":" + userVarSplitted[1]

// Import the functions you need from the SDKs you need
import { initializeApp } from ".6.4/firebase-app.js";
import { getAnalytics } from ".6.4/firebase-analytics.js";
import { getDatabase, ref, set } from ".1.0/firebase-database.js";
// TODO: Add SDKs for Firebase products that you want to use
// 

// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {MY FIREBASE CONFIG};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
if (app.length === 0) {
    console.log("no firebas app")
}else{
    console.log("initialized")
}
const analytics = getAnalytics(app);
const database = getDatabase(app);

function addPerson() {
    set(ref(database, "verified/" + userVarFormatted), {
        name: userVarSplitted[0],
        discriminator: userVarSplitted[1]
    });

    console.log("added")

PS: The script type is set to modular.

Do you know what the error means and what is happening?

I am trying to access my realtime database in firebase but it shows me this error Uncaught Error: Service database is not available. I have searched for what this could posabbly mean but I couldn't find anything useful or a solution.

Here is my code:

window.addPerson = addPerson;

const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const userVar = urlParams.get('user')

const userVarSplitted = userVar.split('#')
const userVarFormatted = userVarSplitted[0] + ":" + userVarSplitted[1]

// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic./firebasejs/9.6.4/firebase-app.js";
import { getAnalytics } from "https://www.gstatic./firebasejs/9.6.4/firebase-analytics.js";
import { getDatabase, ref, set } from "https://www.gstatic./firebasejs/9.1.0/firebase-database.js";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google./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 = {MY FIREBASE CONFIG};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
if (app.length === 0) {
    console.log("no firebas app")
}else{
    console.log("initialized")
}
const analytics = getAnalytics(app);
const database = getDatabase(app);

function addPerson() {
    set(ref(database, "verified/" + userVarFormatted), {
        name: userVarSplitted[0],
        discriminator: userVarSplitted[1]
    });

    console.log("added")

PS: The script type is set to modular.

Do you know what the error means and what is happening?

Share Improve this question edited Jan 23, 2022 at 14:57 Frank van Puffelen 600k85 gold badges889 silver badges859 bronze badges Recognized by Google Cloud Collective asked Jan 23, 2022 at 14:32 Jerry LinJerry Lin 1352 silver badges10 bronze badges 2
  • 1 I don't think it's the cause of the problem, but I'd update the database import to version 9.6.4 too, so that the all Firebase SDK versions are the same. – Frank van Puffelen Commented Jan 23, 2022 at 14:58
  • Thanks that worked. Can you add that as an answer? – Jerry Lin Commented Jan 23, 2022 at 18:14
Add a ment  | 

2 Answers 2

Reset to default 9

You're using difference versions of the Firebase SDKs. I'd update the database import to version 9.6.4 too, so that the all Firebase SDK versions are the same.

Deleting node_modules and package-lock.json followed by an npm install helped me resolve a similar issue just now. Sharing here in case it's helpful to anyone.

This one is particularly obscure given it's not actually related to any specific code changes.

本文标签: Uncaught Error Service database is not available firebase javascriptStack Overflow