4

I am trying integrating Firebase with Vue 4 application is currently getting the following error.

Uncaught TypeError: db__WEBPACK_IMPORTED_MODULE_1_.default.database is not a function

I am using "firebase": "^9.0.0" "vue": "^3.0.0"

import { initializeApp } from "firebase/app";
import { getDatabase } from "firebase/database";


const firebaseConfig = {
// keys
};

const app = initializeApp(firebaseConfig);

const db = getDatabase(app);

export {db}

My function call is as below

import {reactive} from 'vue';
import { ref, set, push } from "firebase/database";
import {db} from "./db"


export default {
  setup(){
    const inputUsername = ref("");
    const inputMessage = ref("");
    const state = reactive({
        username: "",
        messages:[]
    });

    const Login = () =>{
      if(inputUsername.value != "" || inputUsername.value != null){
       state.username = inputUsername.value;
       inputUsername.value = "";
      }
    }
    const SendMessage = () => {
    //   const messagesRef = db.database().ref("messages");
      if (inputMessage.value === "" || inputMessage.value === null) {
        return;
      }
      const message = {
        username: state.username,
        content: inputMessage.value
      }
      set(push(ref(db, 'messages')), message);

    //   messagesRef.push(message);
      inputMessage.value = "";
    }
    return{
    inputUsername,
    Login,
    state,
    inputMessage,
    SendMessage
    }
  }
}

I tried the following the following suggestion but it didn't work.

Thank you!

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
V5NEXT
  • 1,907
  • 4
  • 19
  • 38

1 Answers1

2

The new modular SDK does not use namespaces anymore. So you cannot use app.database(). You can simply export a database instance from the file where you initialize Firebase as shown below:

import { initializeApp } from "firebase/app";
import { getDatabase } from "firebase/database";

const app = initializeApp(firebaseConfig);

const db = getDatabase(app);

export {db}

Then import this wherever you need and use it:

import { ref, child, get } from "firebase/database";
import {db} from "../path/to/firebase.js"

const message = {
  username: state.username,
  content: inputMessage.value
}

set(push(ref(db, 'messages')), message);

You can learn more about the new syntax in the documentation


Uncaught TypeError: db._checkNotDeleted is not a function

This error arise due to the fact the you are importing ref from both firebase and vue. Please import Firebase's ref as fireRef.

import { reactive, ref } from "vue";
import { ref as fireRef, set, push } from "firebase/database";
import { db } from "./db";

// usage 
set(push(fireRef(db, "messages")), message);
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • Thank you for the answer, but I am getting an error in this implementation. Uncaught TypeError: db._checkNotDeleted is not a function – V5NEXT Aug 31 '21 at 12:02
  • @V5NXT that sounds like a different issue to me. Your code has no checkNotDeleted function – Dharmaraj Aug 31 '21 at 12:06
  • Are you sure? its a function of the 'db' object. – V5NEXT Aug 31 '21 at 12:09
  • @V5NXT can you share relevant parts of code where that's being used? – Dharmaraj Aug 31 '21 at 12:09
  • I have updated the question with the entire component and your changes. – V5NEXT Aug 31 '21 at 12:13
  • @V5NXT I can't reproduce that issue. Have you tried reinstalling the firebase module and restarting the Vue dev server? If the error still persists can you share a complete screenshot of the error instead of the first line only ? – Dharmaraj Aug 31 '21 at 12:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/236603/discussion-between-v5nxt-and-dharmaraj). – V5NEXT Aug 31 '21 at 12:45
  • I am struggling with the same issue: `db._checkNotDeleted is not a function` on firebase v9.1.3 and vue2. I have moved firebase db initialization into a different file and I import ref as fireRef.. yet no luck:( – lead-free Oct 23 '21 at 08:06
  • @lead-free if that's the case then I'd recommend posting a new question with your code and relevant details – Dharmaraj Oct 23 '21 at 08:11