Hello .
I hope you all doing well .
I have small problem with retrieve data array from firebase … please help me with that
my code from cartView to setData to firebase in documents with user id
sharedData.cartPopular.forEach { (popular) in
orderMenuDetails.append([
"title" : popular.title,
"price" : popular.price,
"quantity" : popular.quantity
])
}
// Auth.auth().currentUser!.uid
db.collection("ORDERS").document(Auth.auth().currentUser!.uid).setData([
"order" : orderMenuDetails,
"total" : sharedData.TotalPrice(),
"name": appStorage.fullName,
"adress": appStorage.homeAdress,
"phone": appStorage.phoneNumber,
"city": appStorage.city,
"order_time": date,
"location": homeDataModel.userAddress
]) { (err) in
if err != nil {
return
}
print("All good")
}
}
and that is how its look in firebase
and the code to retrieve the array data to my app
struct order :Codable {
// var id : String
var price : Int
var quantity : Int
var title : String
}
struct orderData :Identifiable, Codable {
var id : String
var adress : String
var location : String
var city : String
var name : String
// var order_time : Date
var phone : String
var total : String
var orderDetails : [order]?
}
class OrderDataModel: ObservableObject {
@Published var orderDatafb : [orderData] = []
@Published var orderDetails : [order] = []
init() {
fetchData()
}
func fetchData() {
let db = Firestore.firestore()
db.collection("ORDERS").document(Auth.auth().currentUser!.uid).getDocument(completion: { documentSnapshot, error in
if let err = error {
print(err.localizedDescription)
return
}
if let doc = documentSnapshot {
let id = doc.documentID
let adress = doc.get("adress") as! String
let location = doc.get("location") as! String
let city = doc.get("city") as! String
let name = doc.get("name") as! String
let phone = doc.get("phone") as! String
let total = doc.get("total") as! String
let orderDetails = doc.get("order") as! [Any]
self.orderDatafb.append(orderData(id: id, adress: adress, location: location, city: city, name: name, phone: phone, total: total))
for item in orderDetails {
print(item)
}
}
})
and the printing in debug
2022-01-15 17:59:35.464383+0200 DeliveryRestaurant[1548:20383] 8.9.1 - [Firebase/Firestore][I-FST000001] WatchStream (7ff2e0b1a568) unwatch: <ListenRequest 0x7000047572a8>: {
database: "projects/deliveryrestaurant-d81a4/databases/(default)"
remove_target: 14
}
{
price = 33;
quantity = 1;
title = "Beef grilled";
}
{
price = 7;
quantity = 2;
title = "Mash burger";
}
no matter how much I try to append the data to empty array or try to store the retrieve data so I can show it up in content view I always get error some where or back to nil !!
my question is : how can I store the data in array so I can show it up in view …
please I am stuck with this for a 3 days help me with that …
thank you …