Problem implementing the update method in Angular 2+

-1

I'm using MongoDB database. I believe that most understand that to update a record it is extremely important to get the identifying key, that is _id , I am able to get the identifying key of the record doing so;

ngOnInit() {
    const codigoLancamento = this._route.snapshot.params['id'];
 }

But I have no idea how my method will receive this record using the method below;

onSubmit(){
    var id;
    this._restaurantAdminService.editRestaurant( id , this.restaurant).subscribe(
      response => {
        if(!response.restaurant) {
          this.status = 'error';
        } else {
          this.status = 'success';
          this.restaurant = response.restaurant;




        }
    },
      error => {
        var errorMessage = <any>error;

        if(errorMessage != null){
          this.status = 'error';
        }
      }
    );
  }

I am open to any questions you may have to help me resolve this issue.

    
asked by anonymous 14.08.2018 / 14:48

1 answer

1

I believe the only proplem is to consider "constCollection" as a scope variable of ngOnInit . This variable had to be of the scope of the component, for you to access it in the submit.

codigoLancamento;

ngOnInit() {
    this.codigoLancamento = this._route.snapshot.params['id'];
}

onSubmit(){
    var id = this.codigoLancamento;
    this._restaurantAdminService.editRestaurant( id , this.restaurant).subscribe(
      response => {
        if(!response.restaurant) {
          this.status = 'error';
        } else {
          this.status = 'success';
          this.restaurant = response.restaurant;




        }
    },
      error => {
        var errorMessage = <any>error;

        if(errorMessage != null){
          this.status = 'error';
        }
      }
    );
  }
    
14.08.2018 / 15:32