Javascript 财产';更新';和';数量';不';类型'上不存在;{}';
我正在看Mosh Hamedani使用Angular版本6的教程,但问题是教程版本是4。我正在进行AddToCart按钮上的电子商务项目,在该项目中,产品应通过单击按钮增加其数量,并在Firebase中使用productId进行更新,如果我尝试添加新产品,则新产品的id应添加到AngularFire数据库中。 item.update()和item.quantity的最后一行有错误。请通读代码并向我建议更好的解决方案。提前谢谢 这是代码 购物车服务Javascript 财产';更新';和';数量';不';类型'上不存在;{}';,javascript,firebase,firebase-realtime-database,angular6,angularfire,Javascript,Firebase,Firebase Realtime Database,Angular6,Angularfire,我正在看Mosh Hamedani使用Angular版本6的教程,但问题是教程版本是4。我正在进行AddToCart按钮上的电子商务项目,在该项目中,产品应通过单击按钮增加其数量,并在Firebase中使用productId进行更新,如果我尝试添加新产品,则新产品的id应添加到AngularFire数据库中。 item.update()和item.quantity的最后一行有错误。请通读代码并向我建议更好的解决方案。提前谢谢 这是代码 购物车服务 import { Injectable } fr
import { Injectable } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import { Product } from '../model/product';
import { take } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class ShoppingCartService {
constructor(private db: AngularFireDatabase, ) { }
private create() {
return this.db.list('/shopping-cart').push({
dateCreated: new Date().getTime()
})
}
private getCart(cartId: String) {
return this.db.object('/shopping-cart/' + cartId);
}
private getItem(cartId:string, productId: String) {
return this.db.object('/shopping-cart/' + cartId + '/items/' +productId);
}
private async getOrCreateCart() {
let cartId = localStorage.getItem('cartId');
if (cartId) return cartId;
let result = await this.create();
localStorage.setItem('cartId', result.key);
return result.key;
}
async addToCart(product: Product) {
let cartId = await this.getOrCreateCart();
let item$ = this.getItem(cartId, product.key);
item$.valueChanges().pipe(take(1)).subscribe(item => {
// I'am getting error in update() and quantity
item.update({ product: product,quantity: (item.quantity || 0) + 1});
})
}
}
private async getOrCreateCart() {
let cartId = localStorage.getItem('cartId');
if (cartId) return cartId;
let result = await this.create();
localStorage.setItem('cartId', result.key);
return result.key;
}
async addToCart(product: Product) {
let cartId = await this.getOrCreateCart();
let item$ = this.getItem(cartId, product.key);
item$.valueChanges().pipe(take(1)).subscribe(item => {
item$.update({ product: product,quantity: (item.quantity || 0) + 1});
})
}
预期结果是,单击“添加到购物车”按钮后,必须在firebase中更新产品数量
查看我的其他文件(供参考)
1.home.component.html(这里是单击转到.ts文件时的按钮,如下所示)
最后一个文件呢
3.购物车服务
import { Injectable } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import { Product } from '../model/product';
import { take } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class ShoppingCartService {
constructor(private db: AngularFireDatabase, ) { }
private create() {
return this.db.list('/shopping-cart').push({
dateCreated: new Date().getTime()
})
}
private getCart(cartId: String) {
return this.db.object('/shopping-cart/' + cartId);
}
private getItem(cartId:string, productId: String) {
return this.db.object('/shopping-cart/' + cartId + '/items/' +productId);
}
private async getOrCreateCart() {
let cartId = localStorage.getItem('cartId');
if (cartId) return cartId;
let result = await this.create();
localStorage.setItem('cartId', result.key);
return result.key;
}
async addToCart(product: Product) {
let cartId = await this.getOrCreateCart();
let item$ = this.getItem(cartId, product.key);
item$.valueChanges().pipe(take(1)).subscribe(item => {
// I'am getting error in update() and quantity
item.update({ product: product,quantity: (item.quantity || 0) + 1});
})
}
}
private async getOrCreateCart() {
let cartId = localStorage.getItem('cartId');
if (cartId) return cartId;
let result = await this.create();
localStorage.setItem('cartId', result.key);
return result.key;
}
async addToCart(product: Product) {
let cartId = await this.getOrCreateCart();
let item$ = this.getItem(cartId, product.key);
item$.valueChanges().pipe(take(1)).subscribe(item => {
item$.update({ product: product,quantity: (item.quantity || 0) + 1});
})
}
以下是错误图像:
1.错误又回来了
2.现在,当我修改addToCart(product:product)的上述代码时,它是:
async addToCart(product: Product) {
let cartId = await this.getOrCreateCart();
let item$ = this.getItem(cartId, product.key);
item$.snapshotChanges().pipe(take(1)).subscribe((item: any) => {
if(item.key != null) {
item$.update({ product: product,quantity: (item.quantity || 0) + 1});
} else {
item$.set( {product:product, quantity:1});
}
});
}
我发现以下错误:
这就是我所有的。。。请再次查看错误并提出更好的解决方案。。。提前感谢您对从数据库获得的值使用更新方法。必须对数据库对象使用更新方法 我无法测试它,请告诉我它是否有效
async addToCart(product: Product) {
let cartId = await this.getOrCreateCart();
let itemRef = this.getItem(cartId, product.key);
itemRef.valueChanges().pipe(take(1)).subscribe(item => {
itemRef.update({ product: product,quantity: (item.quantity || 0) + 1});
})
}
您的products.component.ts文件是这样的吗 产品.组件.ts
import { ShoppingCartService } from './../shopping-cart.service';
import { Product } from './../models/product';
import { ActivatedRoute } from '@angular/router';
import { ProductService } from './../product.service';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { switchMap } from 'rxjs/operators';
import { Subscription } from 'rxjs';
@Component({
selector: 'app-products',
templateUrl: './products.component.html',
styleUrls: [ './products.component.css' ]
})
export class ProductsComponent implements OnInit, OnDestroy {
products: Product[] = [];
filteredProducts: Product[] = [];
category: string;
cart: any;
subscription: Subscription;
constructor(
route: ActivatedRoute,
productService: ProductService,
private shoppingCartService: ShoppingCartService
)
{
productService
.getAll()
.pipe(
switchMap((products: Product[]) => {
this.products = products;
return route.queryParamMap;
})
)
.subscribe((params) => {
this.category = params.get('category');
this.filteredProducts = this.category
? this.products.filter((p) => p.category === this.category)
: this.products;
});
}
async ngOnInit() {
this.subscription = (await this.shoppingCartService.getCart())
.valueChanges()
.subscribe((cart) => (this.cart = cart));
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
async addToCart(product: Product) {
let cartId = await this.getOrCreateCart();
let item$ = this.getItem(cartId, product.key);
item$.snapshotChanges().pipe(take(1)).subscribe((item) => {
if (item.payload.exists()) {
item$.update({ quantity: item.payload.exportVal().quantity + 1 });
} else {
item$.set({ product: product, quantity: 1 });
}
});
}
这里看一下ngonit()函数。
在.subscribe()之前,必须编写.valueChanges()
这意味着你必须写作
async ngOnInit() {
this.subscription = (await this.shoppingCartService.getCart())
.valueChanges()
.subscribe((cart) => (this.cart = cart));
}
购物车.service.ts
import { ShoppingCartService } from './../shopping-cart.service';
import { Product } from './../models/product';
import { ActivatedRoute } from '@angular/router';
import { ProductService } from './../product.service';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { switchMap } from 'rxjs/operators';
import { Subscription } from 'rxjs';
@Component({
selector: 'app-products',
templateUrl: './products.component.html',
styleUrls: [ './products.component.css' ]
})
export class ProductsComponent implements OnInit, OnDestroy {
products: Product[] = [];
filteredProducts: Product[] = [];
category: string;
cart: any;
subscription: Subscription;
constructor(
route: ActivatedRoute,
productService: ProductService,
private shoppingCartService: ShoppingCartService
)
{
productService
.getAll()
.pipe(
switchMap((products: Product[]) => {
this.products = products;
return route.queryParamMap;
})
)
.subscribe((params) => {
this.category = params.get('category');
this.filteredProducts = this.category
? this.products.filter((p) => p.category === this.category)
: this.products;
});
}
async ngOnInit() {
this.subscription = (await this.shoppingCartService.getCart())
.valueChanges()
.subscribe((cart) => (this.cart = cart));
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
async addToCart(product: Product) {
let cartId = await this.getOrCreateCart();
let item$ = this.getItem(cartId, product.key);
item$.snapshotChanges().pipe(take(1)).subscribe((item) => {
if (item.payload.exists()) {
item$.update({ quantity: item.payload.exportVal().quantity + 1 });
} else {
item$.set({ product: product, quantity: 1 });
}
});
}
我想它会起作用的。请告诉我它是否有效。哇,太棒了。。。update()错误已消失,但“item.quantity”中的数量仍然存在。。。错误是类型“{}”上不存在相同的属性数量。。。但是,这个项目仍然成功地编译了。现在问题是TypeError:无法读取未定义的属性“key”。。。当点击AddtoCartok时,我想我已经找到了你教程的源代码。我认为应该是产品。$key不是产品。key。我已经编辑了答案。谢谢你的回复。。。在Angular 6中,$键替换为Angular 4中的just键。。。好的,问题是它无法通过产品模型从firebase获得值这里是我的产品模型,供您参考导出接口产品{key:string;title:string;price:number;categories:string;imageUrl:string;}我看到您对教程代码做了一些修改。我认为问题可能出在model文件夹中的shopping-cart-item.ts中。在原始代码中没有产品属性。您是否更改了某些内容,但忘记添加quantity属性?谢谢您提供的信息。您在点击事件中使用的
product
变量定义在哪里?您在GitHub上有代码吗?如果您可以共享整个代码,这将更简单。product变量属于product接口模型,它作为参数在component中的addToCart(product:product)方法中传递。。。。关于你的第二个问题。。。不,由于错误,我没有在Github上上载项目,一旦错误消失,我将上载它Github用于软件开发,而不仅仅用于存储完成的代码;)。如果我只看到你代码的一部分,我很难帮助你。但是我认为这个产品
变量出了问题。这应该是您从数据库中加载的实际产品。好的,我将很快将其上载到Github