Experiencing a glitch with the Realtime Database feature on Firebase

    // db.js file

    import * as firebase from "firebase/app"
    import "firebase/database"

    const config = {
        apiKey: "" ...
    }

    const db = firebase.initializeApp(config)
    export default db

    // App.vue file


    import { reactive, onMounted, ref } from 'vue'
    import db from "./db.js";

    const SendMessage = () => {
          const messagesRef = db.database().ref("messages")

          if(inputMessage.value === "" || inputMessage.value === null) {
            return
          }

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

          messagesRef.push(message)
          inputMessage.value = ""
        }

I am working on a chat application using Vue.js and Firebase. However, I encountered an issue when trying to send a message: db_js__WEBPACK_IMPORTED_MODULE_1_.default.database is not a function at Proxy.SendMessage (App.vue?3dfd:63:1)

I suspect the problem lies in the way I imported the database module, but even after attempting to switch to version 9 of Firebase, the error persists.

Answer №1

Firebase V9 embraces a functional approach, which is evident in the way Firestore interactions are handled. I typically receive an indicator if something isn't functioning as expected.

db.js

import { initializeApp } from 'firebase/app';
import { getDatabase, ref, push, child, serverTimestamp } from 'firebase/database'

const config = {
  apiKey: "" ...
}

const firebase = initializeApp(config)
const db = getDatabase()

function addMessage(path, message) {
  message.createAt = serverTimestamp() // The database will automatically assign the current timestamp to this field for sorting purposes.
  return push(child(ref(db, path)), message)
}
export { addMessage }

App.vue file

import { reactive, onMounted, ref } from 'vue'
import { addMessage } from "./db.js";

const SendMessage = async () => {
  if (!inputMessage.value) return 
  // The above line checks if the value is empty or null by returning false and using ! to convert it to true.

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

  const result = await addMessage('path to data in tree', message)
  // The async/await syntax ensures that the code waits for the data to be saved in the database before proceeding.
  console.log(result.key) // If awaited, the result can be accessed if it's not void.
  inputMessage.value = ""
}

If you're using ref() in .vue files, remember that database operations outside of vue files utilize the same ref() function but with different functionality. You also have the option to rename functions during import like so:

import { ref as dbRef } from 'firebase/database'

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The `tailwind.min.css` file takes precedence over `tailwind.css` in my Nuxt

Having trouble configuring Tailwind to use a custom font without it overriding the tailwind.css file and not displaying the changes? https://i.stack.imgur.com/ExDCL.png Check out my files below. // tailwind.config.js const defaultTheme = require('ta ...

Looking to manipulate the form submit data before it is submitted using the standard submit event and jQuery? Read on to learn how to remove, enhance, or change

Is there a way to extract data from a form like the one below? <form action="/search/search" data-remote="true" data-type="json" id="search_form" method="get"> <div class="input-group"> <span class="input-group-addon"> <i ...

Ways to retrieve the length of the parent array within a component in AngularJS

Is there a way to access the value of a property in the parent from within a component? Parent: export class Animal{ animalID ?: Array<number>; { Child: import {Component, Input} from '@angular/core'; import {Animal} from '../anim ...

Loading Web Applications Screen

My web app contains multiple tree views. Upon loading the page, I first see the unordered lists before the styling of the trees is displayed in the DOM after a brief delay. Is there a method to mask the web app with a spinner placed in the center of the s ...

What is the best way to call a function within a vue.js component?

How can I execute function b from within function a in a vue.js component? Here is the code snippet: methods:{ a(){ console.log("a") b(); } b(){ console.log("b") } } ...

Having trouble getting Material-UI radio buttons to cooperate with react-final-form

After successfully implementing my material-ui radio button, I encountered issues when integrating it with react-final-form. The problem now is that the radio button is no longer clickable. However, upon removing the ...input inside inputProps, the radio b ...

Testing the scope of an Angular directive

Encountering an issue with the unit test In my code, I have: describe('test controller', function () { var =$compile, scope, rootScope; beforeEach(module('myApp')); beforeEach(inject(function (_$compile_, _$rootScope_) { ...

Updating the values of parent components in Vue.js 3 has been discovered to not function properly with composite API

Here is a Vue component I have created using PrimeVue: <template lang="pug"> Dialog(:visible="dShow" :modal="true" :draggable="false" header="My Dialog" :style="{ width: '50vw' }" ...

Valums file-uploader: Restricting file uploads based on user's credit score

Currently utilizing the amazing file uploader by Valums, which can be found at https://github.com/valums/file-uploader One feature I am looking to incorporate is a limit based on the user's account balance. The initial image upload is free, so users ...

Guide to deploying a Rails app with Vue on the Heroku free plan

My project consists of a front-end built on Vue.js and a backend developed using Ruby on Rails. Now, I am looking to deploy it on Heroku's Free plan. Can someone provide me with the necessary reference documents or helpful links for guidance? Thank yo ...

React - Updating state from an external component

I realize that the question I am about to ask may go against some basic principles of using React... however, with this example, I hope someone can assist me in finding a solution to the problem I am currently facing. While this code snippet is not from m ...

Version 10.0 of sails is having trouble with an undefined 'schema' when using mysql

i'm currently experimenting with sails js version 0.10.0 using the sails-mysql adapter 0.10.6 I have set up two models: Customer.js module.exports = { connection: 'someMysqlServer', attributes: { name: { type: 'string& ...

Loading an OBJ file from a blob using three.js

I am currently attempting to load an OBJ file from a blob using Three.js. After referring to this resource and successfully loading STL files, I encountered an issue with loading OBJ files. The error message I received is as follows: TypeError: text.indexO ...

What are the steps to set a 404 status code in next.js when utilizing ISR with a fallback of "blocking"?

When a route does not match, what is the best way to redirect to a 404 error page and ensure that the status code is displayed as 404 in the network tab using ISR with fallback: "blocking"? ...

What is the best way to download the entire page source, rather than just a portion of it

I am currently facing an issue while scraping dynamic data from a website. The PageSource I obtain using the get() method seems to be incomplete, unlike when viewing directly from Chrome or Firefox browsers. I am seeking a solution that will allow me to fu ...

Top method for independently scrolling overlapping elements in both the x and y directions

Sorry if this is repeating information. I have a structure of nested divs like this: -container -row In order to enable scrolling without the default scrollbar appearing, each container and row has an additional container. My goal is to be able to scrol ...

Launch the Image-Infused Modal

I am completely new to the world of Ionic development. Currently, I am working on a simple Ionic application that comprises a list of users with their respective usernames and images stored in an array. Typescript: users = [ { "name": "First ...

Implementing CKEditor instances within AngularJS Controller scopes

Greetings everyone, Recently, I have been exploring Angular JS. Everything is going smoothly - my controllers, services, and so on. However, when attempting to make a Div editable that is within the ng-controller, the ckeditor toolbar fails to appear. On ...

Chrome causing window.open to fail

I am currently working on developing a Cordova PhoneGap app and facing an issue with popping up a window for Facebook login after clicking a button. The popup works fine on iOS devices, but when I try debugging with Chrome, nothing happens even though th ...

Finding the variable with the highest value using AngularJS

I have a variety of variables with different values assigned to them, such as: Weight_gain = 0.01 Weather_gain = 0.02 and many more like these. My goal is to determine the variable name with the highest assigned value. When I attempt this using code, it ...