I'm using json-server (version 1.0.0-beta.3) my id is not being generated automatically

Discussão em 'Angular' iniciado por vinudev, Dezembro 7, 2024.

  1. vinudev

    vinudev Guest

    I'm using json-server as a fake api, and when making the POST request, my ids are not being generated automatically, note that they are already as a String, which is as recommended in the documentation.

    My class user

    export class User {
    public id: String = ''
    public name: String = ''
    public phone: String = ''
    public email: String = ''
    public cpf: String = ''
    public password: String = ''
    }


    my class in which I assign the form data to user and my method that accesses the post in the service

    onSubmit(): void {

    if(this.addressForm.controls['name'].value)
    this.user.name = this.addressForm.controls['name'].value

    if(this.addressForm.controls['email'].value)
    this.user.email = this.addressForm.controls['email'].value

    if(this.addressForm.controls['phone'].value)
    this.user.phone = this.addressForm.controls['phone'].value

    if(this.addressForm.controls['cpf'].value)
    this.user.cpf = this.addressForm.controls['cpf'].value

    if(this.addressForm.controls['password'].value)
    this.user.password = this.addressForm.controls['password'].value

    console.log(this.user)


    this.service.addUser(this.user).subscribe({
    next: (response) => {
    console.log(response)
    alert('Dado registrado com sucesso')
    },
    error: (erro: any) => { // error -> Tratamento de exceção do subscribe
    console.log(erro)
    alert('Ocorreu algum erro')
    }
    })
    }


    from 10 to 13 I manually put the data in my db.json, after that we can notice that the id is not coming automatically

    {
    "id": "10",
    "firstName": "Bruno",
    "email": "brunohauck@gmail.com",
    "phone": "323",
    "cpf": "05126769685",
    "password": "fdsfds"
    },
    {
    "id": "11",
    "firstName": "Bruno",
    "email": "brunohauck@gmail.com",
    "phone": "323",
    "cpf": "05126769685",
    "password": "fdsfds"
    },
    {
    "id": "12",
    "firstName": "Bruno",
    "email": "brunohauck@gmail.com",
    "phone": "323",
    "cpf": "05126769685",
    "password": "fdsfds"
    },
    {
    "id": "13",
    "firstName": "Bruno",
    "email": "brunohauck@gmail.com",
    "phone": "323",
    "cpf": "05126769685",
    "password": "fdsfds"
    },
    {
    "id": "",
    "name": "teste",
    "phone": "1111111111",
    "email": "teste@gmail.com",
    "cpf": "18361591885",
    "password": "111"
    }
    ]

    Continue reading...

Compartilhe esta Página