【问题标题】:Create a for loop inside the create firebase collection function在 create firebase 集合函数中创建一个 for 循环
【发布时间】:2020-02-23 23:58:04
【问题描述】:

我正在使用 Ionic (4) /Angular 创建一个食品应用程序,它可以处理多个商店并使用 FireBase。但我有一个问题。我像这样创建一个新订单

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,
          }
        });     
      }

要创建一个订单,我需要在上面的函数中使用这样的 for 循环

 for(let i in stores){
items: stores[i].items,
          store: {
            id: stores[i].id,
           name: stores[i].name,
          }
}

但这在 firebase 收集功能中是不可能的……啊!

那么,

这就是我的应用程序的工作方式:

点击结帐按钮

this.orderProvider.add(this.cart.stores, this.address);

我的模型:

购物车.ts

import { CartStore } from "./cart-store";

export class Cart {
  id?: string;
  userId: string;
  total: number;
  stores: Array<CartStore>;
}

订单.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() {
  }
}

谢谢你帮助我!我从这个领域开始。

【问题讨论】:

  • 如果你总是只有一个商店,你可以像stores[0].items等一样直接访问它。否则,在collection.add()调用之前循环商店为每个商店添加一个订单.
  • stores[0] 不起作用,因为它没有引用特定的商店。我把你需要的所有代码都放了,请查看。

标签: angular typescript firebase ionic-framework


【解决方案1】:

你可以使用 for of 循环:

add(stores: Array<CartStore>, address: string) {
    for (const store of stores) {
      if (store && store.items && store.items.length) {
        this.afs.collection<Order>('orders').add({
          userId: this.userId,
          address: address,
          items: store.items,
          store: {
            id: store.id,
            name: store.name
          }
        });
      }
    }
  }

【讨论】:

  • 通过订购 2 件商品,它创建了 17 个订单,其中 2 个订单各有一件。
  • 其他15个订单没有任何商品。
【解决方案2】:

这是我的 store.service.ts

import { Injectable } from '@angular/core';
import { AngularFirestore } from 'angularfire2/firestore';
import { Store } from '../models/store';
import {map} from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class StoreService {

  constructor(public afs: AngularFirestore) {

  }

  all() {
    return this.afs.collection<Store>('stores/').snapshotChanges().pipe(map(actions => {
      return actions.map(a => {
        const data = a.payload.doc.data() as Store;
        const id = a.payload.doc.id;
        return {id, ...data};
      });
    }));
  }

  get(id: string) {
    return this.afs.doc('stores/' + id).valueChanges();
  }
}


这是我的商店.ts

export class Store {
  id?: string;
  name: string;
  address: string;
  itemCount?: number;
  rateCount?: number;
  rating?: number;

  constructor() {
  }
}

我有 2 个应用程序链接到同一个 Firebase,一个用于客户,另一个用于管理员(商店帐户)。我注意到我在 firebase 中只有一个管理员帐户。

【讨论】:

    【解决方案3】:

    我尝试过这样做

    add(stores: Array<CartStore>, address: string) {
        for (let i in stores) {
          if (stores[i] && stores[i].items && stores[i].items.length) {
            this.afs.collection<Order>('orders').add({
              userId: this.userId,
              address: address,
              items: stores[i].items,
              store: {
                id: stores[i].id,
                name: stores[i].name
              }
            });
          }
        }
      }
    

    但这会创建多个订单(针对每个项目),而不是创建一个包含所有项目的单个订单。

    【讨论】:

    • 如果您想按一个顺序创建所有项目,您应该事先迭代项目并创建具有正确结构的对象,因此您只能在一次添加调用中添加此对象。
    • 因为您已经在检查stores[i].items.length,它应该只添加实际上也有项目的订单。您能否发布一个示例数据结构? IE。作为商店传递的内容以添加功能
    • 我刚刚上传了一些东西。
    • 对不起,但这并没有帮助。我在询问您传递给 add(stores: Array, address: string) 方法的输入。您可以在 for 循环之前添加一个 console.log(stores) 并在此处发布输出。
    • 用 for of 循环替换你的 for in 循环。 for in 用于对象,for of 用于数组:)
    猜你喜欢
    • 2018-04-02
    • 2022-08-24
    • 2011-11-15
    • 2021-08-11
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多