vendredi 8 juin 2018

TypeError: Cannot read property 'prod_id' of undefined

Please I have a problem. I need to test a function like code below:

  populateForm() {
    this.activatedRoute.params.subscribe(
      params => {
        this.ws.getbyid(params['id']).subscribe(
          prod=> {
            this.prod= prod;
          this.editprodForm.controls['prodnr'].setValue(prod.prodnr);
            this.editprodForm.controls['proddesc'].setValue(prod.proddesc);
          }
        );
      }
    );
  }
  onupdatprod() {
    this.areWeWaiting = true;
    let updatprod= new Prod(
      this.editprodForm.value
    );
    updatprod.prod_id= this.prod.prod_id;

    this.ws.update(updatprod).subscribe(
      result => {
        if (result === true) {
          Materialize.toast('success', 4000);
        } else {
        }
      },
      error => {
      }
    );
  }

My testing code is like below:

it('should call service.Updatewhen onupdateprod, Validation Error', done => {
    const mock = [];
    spyOn(component['as'], 'UpdateAlarms').and.callThrough()
    let updateprod = new Prod('1231');
    updateprod.prodnr= '4';
    updateprod.proddesc= 'UpdateTest';
    updateprod.prod_id= '1';
    component['ws'].update(updateprod ).subscribe(fail => {
        console.log('Validation Error')
        console.log(fail)
        expect(fail).toBeFalsy();
        done();
    });
    component.onupdatprod();
})

My error is:

TypeError: Cannot read property 'prod_id' of undefined

This error maybe because prod_id I take from getbyid and in function onupdatprod() I call updatprod.prod_id= this.prod.prod_id;

Can you ask me any Idea please, how to read value prod_id

Thanx

Aucun commentaire:

Enregistrer un commentaire