我正在使用Ionic(4)/ Angular创建一个食物应用程序,该应用程序可以处理多个商店并使用FireBase。但是我有一个问题。我这样创建新订单
Error
$ npm test
...
> karma start karma.conf.js
i 「wdm」: Compiled with warnings.
START:
i 「wdm」: Compiling...
× 「wdm」:
ERROR in ./node_modules/chilkat_node8_win32/chilkat.node 1:2
Module parse failed: Unexpected character '�' (1:2)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
@ ./lib/modules/ABC.js 1:14-44
@ ./test/test.js
i 「wdm」: Failed to compile.
要创建一个单订单,我需要在上面的函数中使用一个for循环
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,
}
});
}
但是在firebase收集功能中这是不可能的...啊!
然后
这是我的应用程序的工作方式:
通过点击结帐按钮
for(let i in stores){
items: stores[i].items,
store: {
id: stores[i].id,
name: stores[i].name,
}
}
我的模特:
Cart.ts
this.orderProvider.add(this.cart.stores, this.address);
Order.ts
import { CartStore } from "./cart-store";
export class Cart {
id?: string;
userId: string;
total: number;
stores: Array<CartStore>;
}
Cart-Store.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-Item.ts
import { CartItem } from './cart-item';
export class CartStore {
id?: string;
name: string;
subTotal?: number;
items?: Array<CartItem>;
constructor() {
}
}
感谢您对我的帮助!我是从这个领域开始的。
答案 0 :(得分:0)
您可以使用for循环:
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
}
});
}
}
}
答案 1 :(得分:0)
我试图这样做
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
}
});
}
}
}
但是这会为每个商品创建多个订单,而不是为所有商品创建一个订单。
答案 2 :(得分:0)
这是我的store.service.ts
<div class="container">
<div class="item item-a">A</div>
<div class="item item-b">B</div>
<div class="item item-c">C</div>
<div class="item item-d">D</div>
</div>
这是我的商店。
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();
}
}
我有2个链接到同一Firebase的应用程序,一个用于客户,另一个用于管理员(商店帐户)。我注意到我在Firebase中只有一个管理员帐户。