Problem implementing update method in Angular 2+

Asked

Viewed 32 times

-1

I’m using Mongodb’s data bank. I believe that most understand that to update a record it is of utmost importance to take the key identifier, ie the _id, I am managed to get the registry identifier key by doing so;

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

But I have no idea how my method will receive this record through 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’m open to any questions you need to help me solve this problem.

  • Try this.codeLanking = this. _route.snapshot.params['id'];

  • Ai in onsubmit(){ const id=this.codeLancameto;

  • I would like to know who is chasing me and notifying my posts with negative as it is up there on top, that son of a bitch not to say otherwise.

1 answer

1


I believe that the only purpose is to consider "const codigoLancamento" as a variable of scope of ngOnInit. This variable had to be from the scope of the Component, for you to access it in 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';
        }
      }
    );
  }
  • I’ve tried that way, it doesn’t work.

  • 1

    in Ubmit, if you give a console.log(this.codigoLancamento) prints the correct value?

  • worked, thank you very much even, I had made another code made by me that gave 15 lines of code, so you saved 15 lines of code kkkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.