I am unable to update my Angular table

Asked

Viewed 12 times

-1

Guys I’m having a question here, I’m trying to update my table but it’s not updating, says it’s updating but it doesn’t update.

Service:

  updateCliente(cadastro: Cadastro): Observable<any> {
    const httpOptions = {
      headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
    };

    return this.http
      .put<any>(`${this.API_URL}editar/${cadastro._id}`, cadastro, httpOptions)
      .pipe(
        tap((data) => {
          console.log(data);
        })
      );
  }

Interface:

export class Cadastro {
  _id?: any;
  nome: any;
  idade: any;
  email: any;
  senha?: any;
}

edit.componentts.

import { Component, OnInit } from '@angular/core';
import { Cadastro } from '../cadastro';
import { CadastroApiService } from '../services/cadastro-api.service';
import { ActivatedRoute } from '@angular/router';
import { FormBuilder, FormGroup, NgForm, Validators } from '@angular/forms';

@Component({
  selector: 'app-editar',
  templateUrl: './editar.component.html',
  styleUrls: ['./editar.component.css'],
})
export class EditarComponent implements OnInit {
  public id: Array<any> = [];
  _id: any;
  public body: any;
  public cadastro = new Cadastro();
  public liveForm: FormGroup | any;
  idtoUpdate = 1;
  constructor(
    public CadastroApiService: CadastroApiService,
    private route: ActivatedRoute,
    private fb: FormBuilder
  ) {}

  ngOnInit(): void {
    this.liveForm = this.fb.group({
      nome: [null, [Validators.required]],
      idade: [null, [Validators.required]],
      email: [null, [Validators.required]],
    });

    let params = this.route.params.subscribe((parameter) => {
      this.id = parameter.id;
    });

    this.getNome();
  }

  getNome() {
    this.CadastroApiService.getUmNome(this.id).subscribe((dados) => {
      this.cadastro = dados[0];
      console.log('cadastro', this.cadastro);
    });
  }

  update(id: any) {
    this.CadastroApiService.updateCliente(id).subscribe(
      (data) => {
        console.log('Atualizado', data);
      },
      (error) => {
        console.log(error);
      }
    );
  }
}

HTML EDIT

<div class="formulario w-25 p-3">
  <h3 style="text-align: center">Editar Usuario</h3>
  <form [formGroup]="liveForm">
    <div class="form-group">
      <label for="exampleInputEmail1">Nome: </label>
      <input
        type="text"
        class="form-control"
        id="nome"
        value="{{ cadastro.nome }}"
        placeholder="Seu nome"
      />
    </div>

    <div class="form-group">
      <label for="exampleInputPassword1">idade</label>
      <input
        type="text"
        class="form-control"
        id="idade"
        value="{{ cadastro.idade }}"
        placeholder="Idade"
      />
    </div>

    <div class="form-group">
      <label for="exampleInputPassword1">E-mail</label>
      <input
        type="email"
        class="form-control"
        id="email"
        value="{{ cadastro.email }}"
        placeholder="Informe seu e-mail"
      />
    </div>

    <button
      (click)="update(cadastro)"
      style="margin-top: 10px"
      class="btn btn-dark"
    >
      Salvar Usuarios
    </button>
  </form>
</div>
  • Please edit the question to limit it to a specific problem with sufficient detail to identify an appropriate answer.

  • If the problem is to "update the database table", you need to see the backend code. You mention "say you’re upgrading but it doesn’t update", you notice how hard it is to understand what it even means, for someone who hasn’t made your system?

No answers

Browser other questions tagged

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