Reputation: 59
I have nested firebase data and in this data I have auto generated ids but I need to take child node's data of this auto generated ids and I have to use these ids in my query. I need to take ordersTerminal/kullanicilarTerminal/userIds(auto generated)/orderIds(auto generated)/isim/username I need to follow that path actually but I can't figure out. How can I make? Here my firebase database:
And here my javascript code:
function userConfig() {
var dataRef = firebase.database().ref('ordersTerminal').child("kullanicilarTerminal");
dataRef.on('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childData = childSnapshot.val();
console.log(childData);
});
});
}
Upvotes: 0
Views: 183
Reputation: 80924
To get the userId of the currently logged in user, you can do the following:
let user = firebase.auth().currentUser;
let uid = user.uid;
To get the autogenerated id, you can do the following:
dataRef.on('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childData = childSnapshot.val();
var key = childSnapshot.key;
console.log(childData);
});
});
Your reference is at node kullanicilarTerminal
, if you use the property key
inside the forEach
, you will get the following id 93ybLDezrCW2pJsDkZbH6IJfq03
If 93ybLDezrCW2pJsDkZbH6IJfq03
is the id of the currently logged in user, then you can do the following:
dataRef.child(uid).on('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childData = childSnapshot.val();
var key = childSnapshot.key;
console.log(childData);
});
});
Now childSnapshot.key
will return 01032020-05032020-2
If you dont have both ids, then you can get the data by doing the following:
dataRef.on('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
childSnapshot.forEach(function(orderSnapshot) {
console.log(orderSnapshot.key); //01032020-05032020-2
console.log(orderSnapshot.val());
})
})
});
});
Your reference is at node kullanicilarTerminal
, if you use the property key
inside the first forEach
, you will get the following id 93ybLDezrCW2pJsDkZbH6IJfq03
, then you do another for loop and retrieve the second id 01032020-05032020-2
and the details using val()
.
Upvotes: 1