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.
Related
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;
})
)
I have been using ForEach to populate my HTML table.
So far so good but the table is not realtime. I have to reload the function for it to fetch the results again. If I add or delete an entry nothing happens VISUALLY until I reload.
Is there a way to make this realtime?
Code from Firebase Docs:
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();
});
});
Please excuse my poor knowledge on JS, I am working on it.
By using once() you're telling that database that you only want to get the current value and don't care about updates.
The solution to get realtime updates, is to use on(). Since a promise can only resolve once while an on() handler is called for every update, you should use a callback with on():
var query = firebase.database().ref("users").orderByKey();
query.on("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();
});
}, function(error) {
console.error(error);
});
If you care about updating a UI in response to such updates, you'll probably want to use child_ handlers. These get called one level lower in your JSON tree, so in your case for each user that is added/changed/deleted. This allows you to update the UI more directly. For example, the child_added event for the above could be:
var query = firebase.database().ref("users").orderByKey();
query.on("child_added", function(snapshot) {
var key = snapshot.key;
var data = snapshot.val();
// TODO: add an element to the UI with the value and id=key
});
}, function(error) {
console.error(error);
});
Now you can handle the other events with:
query.on("child_changed", function(snapshot) {
// TODO: update the element with id=key in the update to match snapshot.val();
})
query.on("child_removed", function(snapshot) {
// TODO: remove the element with id=key from the UI
})
This and much more is covered pretty extensively in our guide for web developers and in the reference documentation.
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
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
});
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();
});