Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

firebase.getDatabase()).ref() is not a function

Hello I'm doing a project shcool on nodeJS and work with firebase. I try to get the name of a users to not rewrite an existing value. the db is:

the db

my code to try something:

serv.js

const firebaseConfig = {
  apiKey: process.env.REACT_APP_API_KEY,
  authDomain: process.env.REACT_APP_AUTHDOMAIN,
  databaseURL: process.env.REACT_APP_DATABASEURL,
  projectId: process.env.REACT_APP_PROJECTID,
  storageBucket: process.env.REACT_APP_STORAGEBUCKET,
  messagingSenderId: process.env.REACT_APP_MESSASGINGSENDERID,
  appId: process.env.REACT_APP_APPID,
  measurementId: process.env.REACT_APP_MEASUREMENTID
};

const {initializeApp} = require('firebase/app');
const {getDatabase} = require('firebase/database');
const {getAuth, signInWithEmailAndPassword, createUserWithEmailAndPassword} = require('firebase/auth');

const fireapp = initializeApp(firebaseConfig);
const db = getDatabase(fireapp);
var firebaseauth = getAuth(fireapp);
[...]

function create_entry_name(name, email)
{
  const ref = db.ref('/users/' + name);

  ref.on('value', (snapshot) => {
    console.log(snapshot.val());
  }, (errorObject) => {
    console.log('The read failed: ' + errorObject.name);
  });
}

that's litteraly the example on the firebase website.

But I get an error witch is:

db.ref is not a function

I will really be thanks full if you have an solution for this problem...

like image 515
scorval Avatar asked Jul 21 '26 05:07

scorval


1 Answers

You are using Modular SDK (v9.0.0+) but using syntax of older name-spaced syntax. Try refactoring the code as follows:

import { getDatabase, ref, onValue} from "firebase/database";

function create_entry_name(name, email) {
  const ref = ref(db, '/users/' + name);

  onValue(ref, (snapshot) => {
    const data = snapshot.val();
    console.log(data)
  });
}

You can find more details about the new syntax in the documentation.

like image 52
Dharmaraj Avatar answered Jul 24 '26 02:07

Dharmaraj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!