Home > OS >  how to read Realtime firebase data (ref.on is not a function )?
how to read Realtime firebase data (ref.on is not a function )?

Time:11-27

use next.js

update and set work without problems, but when I want to read using on, an error occurs, in the settings firebase checked for me read and write = true

import { getDatabase,ref,set,once,on,update,onChildAdded} from 'firebase/database';


const reference = ref(db, 'room/'   'new');
  
  // get support data from firebase
  reference.on('value',function (snapshot) {
      console.log("In Value");
      console.log(snapshot.val());
  }, function(error) {
      console.error(error);
  });
  

I get an error


reference.on is not a function at startSubmit

enter image description here

my firebase

enter image description here

CodePudding user response:

There is a onValue() function in the new Modular SDK to listen for updates at the database reference. Try refactoring the code like this:

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

const reference = ref(db, 'room/'   'new');

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

Checkout the documentation for more information.

  • Related