Issue with nextJS/Firebase database while experimenting with enabling web frameworks

I encountered an issue with a nextJS app I built on Firebase, following the steps and running the commands mentioned below:

 % npx create-next-app@latest
 % cd myapp
 % firebase experiments:enable webframeworks
 % npm install -g firebase-tools
 % firebase init hosting
 % firebase deploy
 % npm i firebase

 % firebase --version
13.4.1

This is the content of app/page.tsx:

 % cat app/page.tsx 
import Image from "next/image";
import LoadData from './components/loadData'

export default function Home() {
  return (
    <main className="flex min-h-screen flex-col items-center justify-between p-24">
      Where shall we go ?
      <LoadData/>
    </main>
  );
}
 % 

Here is the content of app/components/loadData.tsx:

 % cat app/components/loadData.tsx 
import { useState, useEffect } from 'react';
import firebase from 'firebase/compat/app';

export default function LoadData() {
  // const dbRef = firebase.database (This produces no error)
  const dbRef = firebase.database()
  //const dbRef = firebase.database().ref('Restaurant')//.child('Adventure')
  /*dbRef.push().set({
    name: 'My favorite one',
    phone: '00-6969-1122',
    station: '2nd Street North"'
  })*/

  return (
        <div>
      LoadData
        </div>
  )
} /* End of LoadData */
 % 

And here's the file (initFirebase.tsx):

import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/database';

// Note about the following. To be able to log in, only the apiKey is necessary.
// The rest of the information is only useful to access the database.
const firebaseConfig = {
  apiKey: process.env.NEXT_PUBLIC_WEB_API_KEY,
  projectId: process.env.NEXT_PUBLIC_PROJECT_ID,
  databaseURL: process.env.NEXT_PUBLIC_DATABASE_URL
};

firebase.initializeApp(firebaseConfig);

export default firebase;

export {firebaseConfig}

Upon executing the command:

% firebase deploy

This was the response received:

.......
TypeError: e$.database is not a function
......
> Export encountered errors on following paths:
    /page: /

Error: An unexpected error has occurred.

After some experimentation, I identified that this line:

const dbRef = firebase.database()

is causing the issue. However, I am unsure how to address it.

Any assistance or relevant guidance would be greatly appreciated.

(Ultimately, my goal is to successfully access my realtime database for read/write operations)

Answer №1

Start by setting up and using Firebase products by creating a folder called firebase and adding a file named config.js

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

const firebaseConfig = {
    apiKey: "...",
    authDomain: "...",
    databaseURL: "...",
    projectId: "...",
    storageBucket: "...",
    messagingSenderId: "...",
    appId: "..."
  };

const firebaseApp = initializeApp(firebaseConfig);
export const firebaseDatabase = getDatabase(firebaseApp);

In your component, utilize the Firebase product instance imported from firebase/config.js and bring in any other necessary methods or modules from the Firebase SDK

import { firebaseDatabase } from '../firebase/config';
import { push, ref } from "firebase/database";

export default function Home() {

  const writeData = () => {
    push(ref(firebaseDatabase, 'items'), {
      name: "itemName"
    });
  }

  return (
    <button onClick={writeData}>Write Data</button>
  )
}

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

Discover every user receiving a database object instead of individual records with mLab

While using my express application with the mLab database, I encountered an issue. When trying to find only one record, everything works fine. However, when attempting to retrieve a list of users from the database, I receive the following response. Query ...

Is it possible to access API services from a different backend server in Next.js without utilizing the default api route?

I have Nextjs set up to handle APIs with a default route "api" in my pages folder, but I want to use endpoints from a different backend server address (eg: api.domain.com) instead of the default "api" route. Is it possible to do that? ...

Encountering a "window not defined" error while implementing Leaflet in my Nuxt JS application

I'm encountering an issue while trying to generate my nuxt app, specifically on my 'Area' page. It seems like the error is related to the leaflet maps being used on this page. Initially, I attempted to resolve this problem by enclosing my a ...

Having trouble getting rounded-lg and rounded-md to work properly with Tailwind CSS - any ideas why?

I'm currently working with Next.js version 14.2.4 and tailwindcss version 3.4.1, and I've encountered an issue where rounded-lg and rounded-md classes are not applying any border radius. Oddly enough, other classes like rounded-xl and just rounde ...

The wallet disconnects from the next.js Solana application

After following a YouTube tutorial on creating a Solana application, my code is working fine and the wallet successfully connects. However, upon refreshing the page, the wallet disconnects and the getWallet function returns nothing (connected = false and P ...

Steps for referencing a custom JavaScript file instead of the default one:

Currently, I am utilizing webpack and typescript in my single page application in combination with the oidc-client npm package. The structure of the oidc-client package that I am working with is as follows: oidc-client.d.ts oidc-client.js oidc-client.rs ...

The error message "Type Error: val.toString is not a function - mysql npm" indicates

I'm encountering an issue with the 'mysql' package in NPM on a nodeJS backend and I'm puzzled by this error message: TypeError: val.toString is not a function at Object.escape (/Applications/MAMP/htdocs/nodeJS_livredor/node_modules/sql ...

Creating a map in Typescript initialized with a JSON object

In my Typescript class, there is a map that I'm trying to initialize: public map:Map<string,string>; constructor() { let jsonString = { "peureo" : "dsdlsdksd" }; this.map = jsonString; } The issue I'm encounte ...

The hyperlink and criteria provided are accurate, however, the webpage remains stagnant

My login page is supposed to redirect to the contact confirmation page when I receive a 200 response from the server, but it's not working. Can you help me find my mistake? const [form, setForm] = useState({}); async function checkLogin() { tr ...

Bringing the Jquery cursor into focus following the addition of an emoji

Looking to add emojis in a contenteditable div but facing an issue with the cursor placement. Here is a DEMO created on codepen.io. The demo includes a tree emoji example where clicking on the emoji adds it to the #text contenteditable div. However, after ...

What causes these queries to function separately but fail when combined?

I am facing an issue with combining 2 queries in my Firestore collection. These are the 2 examples that work: database .collection('servicebonnen') .where('medewerker', '==', 'CEES') and: database .collecti ...

jQuery does not allow for text input values to be posted

Having an issue with a form using the POST method. I have some PHP controls that are being calculated in jQuery. While all the form control values are accessible in the next form using POST, the values added through jQuery are not posting to the next form. ...

What is the best way to send a string without using quotation marks as a parameter in a function?

If I have an API that takes the query as http://localhost:8000/api/v1/rental/?place__startswith=kathmandu, how can I implement a generic search in ReactJS? What I attempted to do is pass the default parameter as search(query=kathmandu) so that the result ...

What is the best way to showcase a route parameter in the context of Vue.js and Firebase integration

I am currently working on a blog project that offers free sewing patterns as content. My approach involves using route parameters to fetch each blog individually. However, I have encountered an issue where the blog's data is not being retrieved from f ...

Does it have .hasOwnProperty and a significant value?

Today, I encountered a tricky situation involving the JavaScript Object.hasOwnProperty method. I was working on a form that creates properties on an object. The issue arose when dealing with a select box that had a value selected and then reset back to it ...

What are the steps to implement session storage within a Puppeteer automation script?

Currently, I am attempting to retrieve the encounter id from a Puppeteer script that launches a browser. These scripts are executed on AWS synthetic canaries. However, when running the script, I encounter the following error: sessionStorage is not define ...

Discovering the significance of a function's scope

I'm a bit confused about how the answer is coming out to be 15 in this scenario. I do understand that the function scope of doSomething involves calling doSomethingElse, but my calculation isn't leading me to the same result as 15. function doSo ...

Tips for maintaining accessibility to data while concealing input information

Is it possible to access data submitted in a form even if the inputs were hidden by setting their display to none? If not, what approach should be taken to ensure that data can still be sent via form and accessed when the inputs are hidden? ...

How can I manage file input within a Vue.js application?

After attempting to open a file input with a button, I encountered an issue. When clicking the button, the client reported: “this.$refs.image.click”. Below is my code snippet: <v-btn height="50" ...

What could be causing the EBUSY: resource busy or locked, open errno: -4082 error to appear while trying to build storybook 7 in next.js 13?

While attempting to create a storybook, I encountered the following error in my project: [Error: EBUSY: resource busy or locked, open 'C:\Users\ali\Desktop\Works\Design Systems\projects\storybook-test2\node_modu ...