Home > Software design >  firebase.getDatabase()).ref() is not a function
firebase.getDatabase()).ref() is not a function

Time:11-20

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

var firebaseauth = require('./firebase.js');

var firebaseApp = require('firebase/app');
var firebaseAuth = require('firebase/auth');
var firebaseData = require('firebase/database');
[...]

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

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

And firebase.js

var firebaseapp = require('firebase/app');
var firebase = require('firebase/auth');
require('dotenv').config();

const firebaseConfig = {
  it works dont worry
};

const app = firebaseapp.initializeApp(firebaseConfig);
const auth = firebase.getAuth(app);
module.exports = auth

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...

CodePudding user response:

Call the getDatabase directly. It's already initialized.

const db = getDatabase();
const ref = db.ref('/users/'  name);

CodePudding user response:

Build the app from the app library...

import { firebaseApp } from 'firebase/app';
const app = initializeApp({/*config*/});

Then get the database from the initialized app. Note that the OP fails to pass a param to getDatabase...

const db = getDatabase(app);

const ref = db.ref('users/'   name);
// etc...
  • Related