我的验证存在问题,在实现该函数后,saveProduct()函数将要存储的数据不会添加或更新到数据库。另外,我共享了产品服务文件,因为有人说还必须在服务文件中传递“ this.productForm.value”。但是我不太确定怎么做。
下面是代码:details.page.ts
ngOnInit() {
this.productForm = this.fb.group({
productName: new FormControl('', Validators.compose([
Validators.required,
])),
productDecs: new FormControl('', Validators.compose([
Validators.required,
])),
});
}
async saveProduct() {
await this.presentLoading();
this.product.userId = this.authService.getAuth().currentUser.uid;
if (this.productId) {
try {
console.log('product update');
console.log(this.productForm.value);
await this.productService.updateProduct(this.productId, this.productForm.value);
await this.loading.dismiss();
this.navCtrl.navigateBack('/vendor-tabs/home-vendor');
} catch (error) {
console.log('product dont update');
this.presentToast('Error trying to save');
this.loading.dismiss();
}
} else {
this.product.createdAt = new Date().getTime();
try {
console.log(this.productForm.value);
console.log('product add');
await this.productService.addProduct(this.productForm.value);
await this.loading.dismiss();
this.navCtrl.navigateBack('/vendor-tabs/home-vendor');
} catch (error) {
console.log('product dont add');
this.presentToast('Error trying to save');
this.loading.dismiss();
}
}
}
下面是product.service.ts的代码
addProduct(product: Product) {
return this.productsCollection.add(product);
}
getProduct(id: string) {
return this.productsCollection.doc<Product>(id).valueChanges();
}
updateProduct(id: string, product: Product) {
return this.productsCollection.doc<Product>(id).update(product);
}
deleteProduct(id: string) {
return this.productsCollection.doc(id).delete();
}