I am new in javascript and firebase, I have created single web page in that I want to listen child_changed event below is my code
<!DOCTYPE html>
<html>
<body>
<script src="https://www.gstatic.com/firebasejs/3.0.2/firebase.js"></script>
<script>
var config = {
apiKey: "AIzaSyAqPnuP-EwU2WXreJ_3Ks_er0KbTKE-BNg",
authDomain: "hellofirebase-a1794.firebaseapp.com",
databaseURL: "https://hellofirebase-a1794.firebaseio.com"
};
firebase.initializeApp(config);
var commentsRef = firebase.database().ref().child('hellofirebase-a1794');
commentsRef.on('child_changed', function(data) {
window.alert("sometext"+data.val());
});
</script>
</body>
</html>
My database structure is like below
hellofirebase-a1794
|
|
------betteryLevel : 12
but I am not getting event when I change value from Firebase Console, also not getting any error in console of chrome, anybody please help me what is the problem
Change your code from
var commentsRef = firebase.database().ref().child('hellofirebase-a1794');
to:
var commentsRef = firebase.database().ref().child('/');
You dont need to add hellofirebase-a1794
(your root) to your database reference.
It looks like hellofirebase-a1794
is your project name or is the root, so you do not need to give child after .ref()
.
You can get your database reference as:
var commentsRef = firebase.database().ref();
Then you can get your data from commentsRef
.
This code for use in Node.js
const { getDatabase, ref, onChildAdded } = require('firebase/database'); const db = getDatabase(); onChildAdded(ref(db, 'compliments/received/' + currentUser._id), (snapshot) => { console.log(snapshot.val()) })
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.