Issue
I tried to get all entries from my Firebase database structure in Ionic.
Can someone please help me?
I tried it this way, but I does not work.
firebase.database().ref("Inserate/").get().then((res) => {
if (res.exists()) {
res.forEach(data => {
var obj = data.val();
console.log(obj.city);
});
}
});
This is my realtime database structure in firebase:
- Inserate
- User ID 1
- Unique Key ID 1
- city
- country
- state
- ...
- Unique Key ID 2
- city
- country
- state
- ...
- User ID 2
- Unique Key ID 3
- city
- country
- state
- ...
- Unique Key ID 4
- city
- country
- state
- ...
Solution
It seems you have two nested dynamic levels under Inserate
, first the level for the users, and then for each user a list of nodes.
Your code only handles the first dynamic level, so your data.val()
contains the information user for a user. And a user doesn't have a city
property, which explains why it doesn't show that.
To fix this, you'll need to have two nested loops:
firebase.database().ref("Inserate/").get().then((res) => {
if (res.exists()) {
res.forEach(user => {
user.forEach(data => {
var obj = data.val();
console.log(obj.city);
});
});
}
});
Answered By - Frank van Puffelen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.