Я не могу извлечь объекты MenuItem, называемые line_items, из Firestore.Вот консоль, показывающая, что есть у Firebase:
document.data() is ["status": PENDING, "account_id":
0EE2S7CZeLVMbJ5fujwgKqUHGp53, "total_amount": 544,
"balance_amount": 544, "date": 1553540151000, "discount_amount": 0,
"notes": , "tip_amount": 0, "reward_amount": 0, "balance_id":
QaC1hQwgAHHlCP0rAvgR, "subtotal": 500, "line_items": <__NSArrayM
0x6000029184e0>(
{
basePrice = 100;
category = Beans;
description = "La Colombe 2.2 Kg Bag";
itemId = RWQ7QDTACJRTLHGXX3WNUQHE;
modifierKeys = (
NNZVHCFWVUT5DDQMJ5BOYHXS,
VXVVAAVTLFVPCA66EGTWHBTX
);
name = "La Colombe 2.2Kg BAG";
photoUrl = "https://square-production.s3.amazonaws.com/files/d13f686c4b8995914642f9ddd61491182e9bcd7f/original.jpeg";
quantity = 2;
size = Regular;
sizeAddOnPrice = 150;
toppingsAddOnPrice = 0;
totalModPrice = 150;
totalPrice = 250;
unitPrice = 250;
}
), "tax_amount": 44, "location_id": 0M8QD6ZY890RT]
Вот функции, отвечающие за получение данных транзакции.Первые две работы.Третий нет.Он возвращает line_items как пустой словарь, когда он должен иметь значения.
public func getTransactionData(completion: @escaping (([Order]?) -> ())) {
guard let userId = Auth.auth().currentUser?.uid else { completion(nil); return }
let db = Firestore.firestore()
let query = db.collection("order").whereField("account_id",
isEqualTo: userId)
query.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
guard let querySnapshot = querySnapshot else {
completion(nil); return }
var orders = [Order]()
for document in querySnapshot.documents {
let order = self.extractOrder(document)
if let order = order {
orders.append(order)
}
}
completion(orders)
}
}
}
private func extractOrder(_ document: QueryDocumentSnapshot) -> Order? {
print("document.data() is \(document.data())")
let lineItems = extractLineItems(document.data()["line_items"] as? [[String:Any]] ?? [[:]])
let orderId = document.documentID
guard let balanceId = document.data()["balance_id"] as? String,
let accountId = document.data()["account_id"] as? String,
let subtotal = document.data()["subtotal"] as? Int,
let date = document.data()["date"] as? Int,
let totalAmount = document.data()["total_amount"] as? Int,
let notes = document.data()["notes"] as? String,
let rewardAmount = document.data()["reward_amount"] as? Int,
let status = document.data()["status"] as? String,
let tax = document.data()["tax_amount"] as? Int,
let tip = document.data()["tip_amount"] as? Int,
let balanceAmount = document.data()["balance_amount"] as? Int,
let discountAmount = document.data()["discount_amount"] as? Int,
let locationId = document.data()["location_id"] as? String
else { return nil }
let order = Order(totalAmount: totalAmount, subtotal: subtotal, discountAmount: discountAmount, tipAmount: tip, taxAmount: tax, balanceAmount: balanceAmount, rewardAmount: rewardAmount, balanceId: balanceId, accountId: accountId, locationId: locationId, date: date, status: status, orderType: "PICK UP", lineItems: lineItems, notes: notes, orderId: orderId)
print("order is \(order)")
return order
}
var modifiers: [Modifier]?
var toppings: [String]?
private func extractLineItems(_ dictionaryArray: [[String:Any]]) -> [MenuItem] {
var lineItems = [MenuItem]()
let count = dictionaryArray.count
for x in 0..<count {
guard let itemId = dictionaryArray[x]["item_id"] as? String,
let category = dictionaryArray[x]["category"] as? String,
let name = dictionaryArray[x]["name"] as? String,
let description = dictionaryArray[x]["description"] as? String,
let photoUrl = dictionaryArray[x]["photoUrl"] as? String,
let basePrice = dictionaryArray[x]["base_item_price"] as? Int,
let unitPrice = dictionaryArray[x]["unit_price"] as? Int,
let totalPrice = dictionaryArray[x]["total_price"] as? Int,
let quantity = dictionaryArray[x]["quantity"] as? Int,
let size = dictionaryArray[x]["size"] as? String,
let modifierKeys = dictionaryArray[x]["modifierKeys"] as? [String],
let sizeAddOnPrice = dictionaryArray[x]["sizeAddOnPrice"] as? Int,
let toppingsAddOnPrice = dictionaryArray[x]["toppingsAddOnPrice"] as? Int
else { continue }
if let modifiers = dictionaryArray[x]["modifiers"] as? [Modifier] {
self.modifiers = modifiers
} else {
self.modifiers = nil
}
if let toppings = dictionaryArray[x]["toppings"] as? [String] {
self.toppings = toppings
} else {
self.toppings = [String]()
}
let totalModPrice = sizeAddOnPrice + toppingsAddOnPrice
let lineItem = MenuItem(itemId: itemId, name: name, modifiers: self.modifiers, photoUrl: photoUrl, quantity: quantity, basePrice: basePrice, unitPrice: unitPrice, totalPrice: totalPrice, totalModPrice: totalModPrice, sizeAddOnPrice: sizeAddOnPrice, toppingsAddOnPrice: toppingsAddOnPrice, description: description, size: size, toppings: self.toppings, category: category, modifierKeys: modifierKeys)
lineItems.append(lineItem)
}
return lineItems
}