gpt4 book ai didi

javascript - 类型 'update' 上不存在属性 'Observable<{}>'。火力地堡 5 Angular 火 2 5

转载 作者:太空狗 更新时间:2023-10-29 17:49:17 32 4
gpt4 key购买 nike

我正在尝试在 firebase 中创建/更新购物车。我正在使用具有将 localStorage ID 添加到 firebase 的功能的服务,如果产品已经存在,它会添加购物车中的数量,否则它会创建新的。错误发生在控制台 TypeError: Cannot read property 'quantity' of null 并且我在 shopping-cart service.ts 中的编译也有错误:

  1. 属性“update”在“Observable<{}>”类型上不存在。
  2. 属性“数量”在类型“{}”上不存在

下图展示了我试图在 firebase 中获取的内容: enter image description here

购物车.service.ts

import { take } from 'rxjs/operators';
import { AngularFireDatabase, snapshotChanges } from 'angularfire2/database';
import { Injectable } from '@angular/core';
import { Product } from './models/product';

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

constructor(private db: AngularFireDatabase) { }

private create(){
console.log('shoping service')
return this.db.list('/shopping-carts').push({
dateCreated: new Date().getTime()
});
}

private getCart(cartId: string){
return this.db.object('/shoping-carts/'+ cartId);
}

private async getOrCreateCartId(){
let cartId = localStorage.getItem('cartId');

if(cartId) return cartId;

let result = await this.create();
localStorage.setItem('cartId', result.key);
return result.key;

}
private getItem(cartId: string, productId: string){
return this.db.object('/shopping-carts/' + cartId + '/items/' + productId).valueChanges();
}

async addToCart(product: Product){
let cartId = await this.getOrCreateCartId();
let item$ = this.getItem(cartId, product.key);

item$.pipe(take(1)).subscribe( item => {
item$.update({ product: product, quantity: (item.quantity || 0) + 1});
});
}

shoppng-cart.service.ts(文档的相关部分)

private getItem(cartId: string, productId: string){
return this.db.object('/shopping-carts/' + cartId + '/items/' + productId).valueChanges();
}

async addToCart(product: Product){
let cartId = await this.getOrCreateCartId();
let item$ = this.getItem(cartId, product.key);


item$.pipe(take(1)).subscribe( item => {
item$.update({ product: product, quantity: (item.quantity || 0) + 1});
});
}

product-card.component.ts

import { ShoppingCartService } from './../shopping-cart.service';
import { Product } from './../models/product';
import { Component, OnInit, Input } from '@angular/core';

@Component({
selector: 'product-card',
templateUrl: './product-card.component.html',
styleUrls: ['./product-card.component.css']
})
export class ProductCardComponent implements OnInit {

@Input('product') product;
@Input('show-actions') showActions = true;
constructor(private cartService:ShoppingCartService) { }

addToCart(product:Product){
this.cartService.addToCart(product);
}

ngOnInit() {
}

}

product-card.component.html

<div *ngIf="product.title" class="card m-auto">
<img class="card-img-top" [src]="product.imageUrl" *ngIf="product.imageUrl" alt="{{ product.title }}">
<div class="card-body pb-0">
<h5 class="card-title">{{product.title}}</h5>
<p>{{product.price | currency: 'USD'}}</p>
</div>
<div class="card-footer p-0 border-top">
<button *ngIf="showActions" (click)="addToCart(product)" class="btn btn-primary btn-block">Add to Cart</button>
</div>
</div>

产品.ts:

export interface Product{
key: string;
title: string;
price: number;
category: string;
imageUrl: string;
}

最佳答案

经过大量的搜索和调试也是Yevgen的一部分| answer 我已经修改了我的代码以摆脱错误类型错误:无法读取 null 的属性“数量”。如果我使用 valueChanges,它会在添加新购物车时出错。所以我更改为 snapshotChanges 并放入一些它存在的逻辑并且现在工作正常。如果有人仍然更新我的答案,非常欢迎。

private getItem(cartId:string, productId:string) {
return this.db.object < any > ('/shopping-carts/' + cartId + '/items/' + productId);
}

async addToCart(product:Product) {
let cartId = await this.getOrCreateCartId();
let item$ = this.getItem(cartId, product.key);

item$.snapshotChanges().pipe(take(1)).subscribe((item:any) => {
if (item.key != null) {
item$.update( {quantity:( item.payload.val().quantity || 0) + 1});
}
else{
item$.set( {product:product, quantity:1});
}
});
}

关于javascript - 类型 'update' 上不存在属性 'Observable<{}>'。火力地堡 5 Angular 火 2 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52474408/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com