3

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

4
  • Do you know for sure if you're initializing/stating db properly? Try console logging it and see if it is what you expect. Commented Nov 19, 2021 at 17:39
  • To be honest I don't really know what I expect to get from db, I consoled log him but that doesn't give me a clue... @insyri Commented Nov 19, 2021 at 17:45
  • 1
    ooof there is so much wrong here. Follow the docs here firebase.google.com/docs/database/web/start Commented Nov 19, 2021 at 18:08
  • Hello, so I updated my code with the firebase tuto and your help but I get the exact same error... @BrianMcCall Commented Nov 20, 2021 at 16:49

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It's so frustrating. I see NONE of that new syntax in the docs.
0

Call the getDatabase directly. It's already initialized.

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

1 Comment

Code Updated and same error
0

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

1 Comment

Hello, so I updated my code with the firebase tuto and your help but I get the exact same error...@danh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.