因此,我正在从Mosh Hamedami进行在线课程。我或多或少一直在关注它,但是一切正常。我可以将产品添加到Firebase并进行编辑/删除。现在,我想实现一个“购物车”,在其中可以添加特定的CardId,并向该cardId添加项目+数量
但是我遇到了要在Firebase购物车中添加产品的问题。添加带有cartId的购物车-时间戳有效。我无法以某种方式将商品添加到cartId。
我猜可观察的对象有问题...
控制台错误是:
_core.js:6260 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'key' of undefined
TypeError: Cannot read property 'key' of undefined_
at ShoppingCartService.<anonymous> (shopping-cart.service.ts:51)
它告诉我来自'addToCart'的产品未定义。
addToCart(product: Product) {
this.shoppingCartService.addToCart(product);
console.log(product);
}
[我的编辑者还告诉我,.pipe(take(1))的形式是:
_ error TS2684:类型'void'的'this'上下文不能分配给类型'Observable'的方法'this'。
以某种方式pipe(first())可以工作,但是在密钥方面也存在相同的问题。
我尝试了很多,无法弄清我的错误。我也是Angular的新手。.我真的很感激需要寻找问题的任何技巧。
shopping-cart.service.ts
import {Injectable} from '@angular/core';
import {AngularFireDatabase} from "@angular/fire/database";
import {Product} from "./models/product";
import 'rxjs/add/operator/take';
import {take} from "rxjs-compat/operator/take";
import {pipe} from "rxjs";
import {Observable} from "rxjs";
import {first} from "rxjs/operators";
@Injectable({
providedIn: 'root'
})
export class ShoppingCartService {
constructor(private db: AngularFireDatabase) {
}
//create shopping cart in db + add date(timestamp)
private create() {
return this.db.list("/shopping-carts").push({
dateCreated: new Date().getTime()
});
}
private getCart(cartId: string) {
return this.db.object('/shopping-carts/' + cartId);
}
private getItem(cartId: string, productId: string) {
return this.db.object('/shopping-carts/' + cartId + '/items/' + productId);
}
//getting reference to shopping cart
private async getOrCreateCartId(): Promise<string> {
let cartId = localStorage.getItem('cartId');
if (cartId) return cartId;
// calls this.create and waits for the response
let result = await this.create();
//saves cartId in local storage and returns
localStorage.setItem('cartId', result.key);
return result.key;
}
//add new product to cart and safe localstorage
async addToCart(product: Product) {
const cartId = await this.getOrCreateCartId();
//reference to products in firebase cart
const item$ = this.getItem(cartId, product.key);
item$.snapshotChanges().pipe(take(1)).subscribe((item: any) => {
if (item) {
item$.update({product: product, quantity: (item.quantity || 0) + 1});
}
});
}
}
product.ts
export interface Product {
key : string;
title : string;
price : number;
category : string;
imageURL : string;
}
html调用addToCart:
<div class="columns is-multiline is-narrow">
<ng-container *ngFor="let p of filteredProductsByCategory">
<div class="column is-one-third">
<div class="card">
<figure class="image is-square">
<img [src]="p.imageURL" alt="{{p.title}}">
</figure>
</div>
<div class="card-content">
<p class="title"> {{p.title}}</p>
<p class="subtitle">{{p.price | currency: "USD"}}</p>
<div
(click)="addToCart(product)"
class="button is-outlined is-primary"> Add to Cart</div>
</div>
</div>
</ng-container>
</div>
如果需要,我也可以将我的代码上传到GitHub!谢谢!!
因此,我正在从Mosh Hamedami进行在线课程。我或多或少一直在关注它,但是一切正常。我可以将产品添加到Firebase并进行编辑/删除。现在我要实现一个“ ...
// add new product to cart and safe localstorage
async addToCart(product: Product) { ... }