Retrieving data with firebase for Web - javascript

I am trying to retrieve data from a firebase database :
firebase.database().ref("/appointments").orderByChild("doctor").equalTo(doctorId).on("value", function(snapshot) {
var appointmentsData = snapshot.val();
for(var appointment in appointmentsData) {
if (!appointmentsData.hasOwnProperty(appointment)) continue;
var obj = appointmentsData.appointment;
}
});
If I console.log appointment or console.log appointmentsData, I get the correct value, but if I console.log appointmentsData.appointment, I get undefined.
Any idea how I can retrieve the properties and values from the object that firebase returns ?

You want to use Firebase's built in forEach function. It allows you to iterate through a snapshot and easily get the key and value of each property inside that object. It could be another object or a flat value.
firebase.database().ref("/appointments").orderByChild("doctor").equalTo(doctorId).on("value", function(snapshot) {
snapshot.forEach(function(childSnapshot) {
// key
var key = childSnapshot.key;
// value, could be object
var childData = childSnapshot.val();
// Do what you want with these key/values here
...
});
});
Reference:
forEach, Firebase 3.0

Related

Firebase - Getting first value of last child 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.

How to retrieve data from Firebase using Javascript?

I'm trying to access "id" and store it in a variable. So far the code I have is:
var ref = firebase.database().ref("users");
ref.on("value", function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childData = childSnapshot.val();
console.log(childData);
});
});
What I get from above is:
With that I have access to the object itself, but how can I access the property "id"? I'm reading the official documentation but I can't figure it out.
Do this:
var ref = firebase.database().ref("users");
ref.on("value", function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childData = childSnapshot.val();
var id=childData.id;
console.log(childData);
});
});
In the above the location is at users then you use on() method to read data, and using the forEach loop, you iterate inside the pushid and get the values.
To access each one alone you do this var ids=childData.id; or/and var names=childData.name;
Also it is better to use once() as it only reads data once.
You need to get key of parent-node(L2L6vBsd45DFGfh) to access id child
When you use push() method in firebase it will automatic generate unique key for your record.
use set() or update() method to create your own custom keys.
var ref = firebase.database().ref("users");
ref.on("value", function(snapshot) {
var childData = snapshot.val();
var key = Object.keys(childData)[0]; //this will return 1st key.
console.log(childData[key].id);
});
You will use for-loop or forEach to get all keys.

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;
})
)

Firebase - How to get list of objects without using AngularFire

Firebase - How to get list of objects without using AngularFire
I'm using typescript, angular2 and firebase.
I'm not using angularfire. I want to extract the data using their Api
My firebase url is /profiles
This is the list of profiles I'm looking to extract:
Thanks.
Use a simple value event and re-assign the array every time the value changes.
JSBin Demo.
var ref = new Firebase('https://so-demo.firebaseio-demo.com/items');
ref.on('value', (snap) => {
// snap.val() comes back as an object with keys
// these keys need to be come "private" properties
let data = snap.val();
let dataWithKeys = Object.keys(data).map((key) => {
var obj = data[key];
obj._key = key;
return obj;
});
console.log(dataWithKeys); // This is a synchronized array
});

Firebase get key value

It's easy to get value:
firebase_instance.limit(100).on("child_added", function(data) {
var value = data.val();
});
but how to get a key associated with that value?
Use key property
firebase_instance.limit(100).on("child_added", function(snap) {
var name = snap.key;
});
Documentation and examples here
Each of your Firebase .on(...) callbacks are going to be invoked with a Firebase DataSnapshot. To get the name for the location of that DataSnapshot, try using the name() method.
For example:
firebase_instance.limit(100).on("child_added", function(snap) {
var name = snap.name();
});

Categories