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
- ...
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);
});
});
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.