'Firebase dataSnapshot sub-node
I am trying to snapshot the shopping lists of a user. The snapshot works fine, except for the products node.
Link to db: 
This is my observer:
func observeLists(callBack: @escaping (_ list: [List]) -> Void){
let dbRef = database.child("list").child(ShareData.shared.userId)
dbRef.queryOrdered(byChild: "completed").observe(.value, with: { snapshot in
var newItems: [List] = []
for child in snapshot.children {
if let snapshot = child as? DataSnapshot {
let groceryItem = List(snapshot: snapshot)
newItems.append(groceryItem)
}
}
callBack(newItems)
})
}
And this is my init(snapshot) from struct List
init(snapshot: DataSnapshot) {
let id = snapshot.key
let value = snapshot.value as? NSDictionary
let products = value?["products"] as? [Product] ?? []
let completed = value?["completed"] as? Bool ?? false
let storeId = value?["storeId"] as? String ?? ""
let name = value?["name"] as? String ?? ""
self.init(id, products, completed, storeId, name)
}
Solution 1:[1]
The products node in your database is not an array of products ([Product]), but rather a dictionary/map ([String: Product]).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Frank van Puffelen |
