Firebase - Getting first value of last child Javascript - javascript

Attempting to read the Temp value of the last child in the following Firebase structure, however unable to read the child value.
Firebase Database Structure below:
Attempted code;
//GET last child and return val for temperature;
var lastChild_Light = firebase
.database()
.ref()
.child("av")
.child("data")
.child("01230C3DB6717F1DFE")
.limitToLast(1)
.child("Temp");
lastChild_Light.on("value", function(lastdatasnap) {
document.getElementById("lastTemp").innerText = lastdatasnap.val();
});
The idea is for the user to see the latest temp value, once they land on webpage.

If you are looking Javascript solution the following would work
firebase.database().ref('io/av/data/01230C3DB6717F1DFE/').limitToLast(1).on('child_added', function(snapshot) {
console.log(snapshot.val()); // gives key and value pair - {Temp: "35.43"}
snapshot.forEach((child) => {
console.log(child.val()); // gives value of key
});
});
This could be of more help.

Related

How to update the value of a key in firebase Realtime database (JavaScript)

I want to update the value of the key on firebase Realtime Database but I was not able to update the value my database structure is like this.
{
BoardID:23df2365k87 //want to update the value of BoardID
}
This is the code I am trying
let BoardID = app.ref('BoardID');
app.ref('/BoardID').on('value', (snapshot) =>{
console.log(snapshot.val())
let bordid = snapshot.val();
console.log(bordid);
BoardID.val().update({
'Null'
});
})
I have change the code like this
app.ref("/BoardID").set('two');

Firebase retrieve returns undefined/object Object

I'm currently working on retrieving a child from Firebase with this code:
function fetchUserdetails() {
firebase.database().ref().child('login/').orderByChild('username').equalTo('ivanaldwin').on("value", function(snapshot) {
document.getElementById('fullname').innerHTML = snapshot.val().username;
console.log(snapshot.val()); //debug in console
snapshot.forEach(function(data) {
console.log(data.key);
alert('yep im working dood');
});
});
}
window.onload = fetchUserdetails();
But the code, if i use snapshot.val().username; will render undefined in the output. but if i use snapshot.val() only, the output will be [Object object]. Here's my firebase layout:
To retrieve the username try the following:
snapshot.forEach(function(data) {
let userName = data.val().username;
console.log(data.key);
alert('yep Im working dude');
});
Your snapshot is at node login, therefore after you loop using forEach you then can access the child attributes.
You need to use array index
document.getElementById('fullname').innerHTML = snapshot.val()[0].username;
But I think you would need the element 0 for that one, which you don't have.
Maybe try
document.getElementById('fullname').innerHTML = snapshot.val().child('1').username

Size of child in firebase

I am using react with firebase realtime database
So i have this firebase data and i want to count total values in followers. However, what i know is that firebase do not have such a function to count and you need to manually do it.
From below i can see all the user data such as username and email. However, i still cannot figure out how to count the number of followers.
I hope someone can help. Thank you.
const ref = firebaseDB.ref();
ref
.child("Users")
.orderByChild("email")
.equalTo(this.state.currentUserEmail)
.once("value", snapshot => {
let userData = {};
for (var childSnapshot in snapshot.val()) {
userData.key = childSnapshot;
// then loop through the keys inside the child object and get the values like price (key) : 44.95 (value)
for (var value in snapshot.val()[childSnapshot]) {
console.log(value);
console.log(snapshot.val()[childSnapshot][value]);
userData[value] = snapshot.val()[childSnapshot][value];
}
}
console.log(userData);
});
Something like this should do the trick:
const ref = firebaseDB.ref();
ref
.child("Users")
.orderByChild("email")
.equalTo(this.state.currentUserEmail)
.once("value", snapshot => {
snapshot.forEach((childSnapshot) => {
console.log(childSnapshot.child("followers").numChildren());
}
console.log(userData);
});
The changes/additions:
Use DataSnapshot.forEach() for a cleaner way to loop over the children.
Use DataSnapshot.child() to get the snapshot for the followers node.
Use DataSnapshot.numChildren() to determine the number of followers.

forEach method on Firebase invokes callback with child snapshot with undefined key

It's been a few months I started learning JavaScript, since I am an IOS developer, I prefer Firebase as my backend for my websites too.
So on today's practice, I used to read Firebase data and alert it to myself, I used this code,
Notice: those code's are only examples and used during my work, and it officially comes from Firebase's documentation.
var query = firebase.database().ref("users").orderByKey();
query.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
// key will be "ada" the first time and "alan" the second time
var key = childSnapshot.key;
// childData will be the actual contents of the child
var childData = childSnapshot.val();
alert(key); // also tried the (key.value); as well
});
and here is my Firebase structure:
and the output:
It's funny but firebase doesn't update their docs as often as their API changes, even worse if you're using Angular 4+. Try rewriting your code like this below. you need to return a boolean value after iterating a snapshot with forEach:
var query = firebase.database().ref("users").orderByKey();
query.once("value", (function(snapshot) {
snapshot.forEach(function(childSnapshot) {
// key will be "ada" the first time and "alan" the second time
var key = childSnapshot.key;
// childData will be the actual contents of the child
var childData = childSnapshot.val();
alert(key); // also tried the (key.value); as well
return true;
})
)

Get the value of a key after filtering with value in Firebase

I am new to Firebase. I am trying to get the value of a key after filtering it with a value.
I first get the specific user with the room_num value "323" by using
uref.orderByChild("room_num").equalTo(room_num).once("value" ...
After I get the object of this user, I want to have access to his/her telephone number. But the code below does not give me what I need.
var uref = new Firebase('https://XXX.firebaseio.com/users');
uref.orderByChild("room_num").equalTo("323").once("value", function(data) {
$scope.telephone = data.telephone;
console.log($scope.telephone);
});
I also tried wrapping it with $firebaseArray.
Here is what my database looks like
users
6GFbWBV9rINa3ZCDN45BgM1PO3o2
email: "test#gmail.com"
name: "Mark"
room_num: "323"
telephone: "72839202"
My database looks like this
The callback is passed a snapshot. The snapshot will have a child for each entry that matches the query. To access the data within the child snapshot, you need to call val():
uref.orderByChild("room_num").equalTo("323").once("value", function (snapshot) {
snapshot.forEach(function (childSnapshot) {
var data = childSnapshot.val();
$scope.telephone = data.telephone;
console.log($scope.telephone);
});
});

Categories