I am currently developing a food application using Ionic (4) /Angular that can manage multiple stores and integrates Firebase. However, I have encountered a problem. When creating a new order, I use the following code:
add(stores: Array<CartStore>, address: string) {
this.afs.collection<Order>('orders').add({
userId: this.userId,
address: address,
items: stores.items,
store: {
id: stores.id,
name: stores.name,
}
});
}
To create a SINGLE order, I need to use a for loop within the function as shown below:
for(let i in stores){
items: stores[i].items,
store: {
id: stores[i].id,
name: stores[i].name,
}
}
However, this is not feasible within the Firebase collection function... Frustrating!
Here is an overview of how my app operates:
Users click on the checkout button, initiating the following function:
this.orderProvider.add(this.cart.stores, this.address);
Here are the models utilized in my app:
Cart.ts
import { CartStore } from "./cart-store";
export class Cart {
id?: string;
userId: string;
total: number;
stores: Array<CartStore>;
}
Order.ts
import { CartStore } from "./cart-store";
export class Order {
id?: string;
userId: string;
total?: number;
address: string;
status?: string;
orderNumber?: string;
createdAt?: number;
updatedAt?: number;
store: CartStore;
items: Array<CartStore>;
isReviewed?: boolean;
}
Cart-Store.ts
import { CartItem } from './cart-item';
export class CartStore {
id?: string;
name: string;
subTotal?: number;
items?: Array<CartItem>;
constructor() {
}
}
Cart-Item.ts
import { Variation } from "./variation";
import { ItemReview } from "./item-review";
import { Size } from "./size";
export class CartItem {
id?: string;
name: string;
price: number;
quantity: number;
thumbnail: string;
variations: Array<Variation>;
size: Size;
subTotal: number;
review?: ItemReview;
constructor() {
}
}
I appreciate any assistance provided! I am relatively new to this field.