错误,Reference.push失败:第一个参数包含属性中的undefined

时间:2017-11-14 04:43:42

标签: angular typescript firebase

我是firebase数据库的新手,我正在尝试将数据推送到我的firebase数据库,但是出现了这样的错误Error: Reference.push failed: first argument contains undefined in property 'Product.prdName'

//insert data function in service.ts
insertProduct(Product: Product) {
  this.productList.push({
    prdName: Product.prdName,
    prdCat: Product.prdCat,
    prdSup: Product.prdSup,
  });
}

//onsubmit function in component.ts  
productList: Product[];

constructor(private productService: ProductService) {}

onSubmit(form: NgForm) {
  if (form.value.$key == null) {
    this.productService.insertProduct(form.value);
  } else {
    this.productService.updateProduct(form.value);
  }
  this.resetForm(form);
}

//class.ts
export class Product {
  $key: string;
  prdName: string;
  prdCat: string; //category
  prdSup: string; //supplier
}

<!--form snipped-->

<form #productForm="ngForm" (ngSubmit)="onSubmit(productForm)">
  <input type="hidden" name="$key" #$key="ngModel" [(ngModel)]="productService.selectedProduct.$key">
  
  <div class="form-group">
    <label>Product Name</label>
    <input class="form-control" name="name" #name="ngModel" [(ngModel)]="productService.selectedProduct.prdName" placeholder="Full Name">
  </div>

  <div class="form-group">
    <button type="submit" class="btn btn-primary" (onclick)="">Submit</button> 
  </div>
</form>

是的,有人可以帮助我吗?

如果需要更多代码段,请告诉我们。提前谢谢。

1 个答案:

答案 0 :(得分:1)

您正在传递onSubmit(productForm),因此您将获得类似

的输出
{
  $key : value of productService.selectedProduct.$key
  name : value of productService.selectedProduct.prdName
}

你能做的是

改变

name input属性与Product类相同的值name='name'name='prdName'

OR

this.productService.insertProduct(form.value);

this.productService.insertProduct(this.productService.selectedProduct);