简体   繁体   中英

Axios post with Reactjs - Spring boot

Im very new with Axios, and im trying to do a POST request using Axios and my DB is not getting the data, it creates one entity and all the values are null except the primary key that is 0. Then, i get an error that i'm duplicating the primary key. I don't know if its not getting my JSON or if im doing something else wrong.

api.js

import axios from "axios";

const endpoints = {
 development: 'http://localhost:8080/',
};

export const api = axios.create({
baseURL: endpoints['development'],
timeout: 20000,
 headers: {"Content-type":"application/json" }
});

pacienteService.js

import { api } from './helpers/api.js';

const basePath = 'api';

let config = {
    headers: {
       'Content-Type': 'application/json',
    } 
}

function getAll() { return api.get(`${basePath}/pacientes`); }

function show(pacienteId) { return api.get(`${basePath}/?id=${pacienteId}`); }

function create(data) { return api.post(`${basePath}/pacientes`, data,config); }

const pacientesService = { getAll, show, create };
export default pacientesService;

Function that do the post when Submitting the form

handleSubmit(event){
    const paciente = {rut:this.state.rut,
    nombre:this.state.nombre ,
    nacionalidad:this.state.nombre ,
    sexo:this.state.nombre ,
    fecha_na:this.state.nombre ,
    domicilio:this.state.nombre ,
    diagnostico:this.state.nombre ,
    telefono:this.state.nombre ,
    gravedad:this.state.recuperacion
    }
      
       pacientesService.create({paciente}).then(res => {
        console.log(res);
        console.log(res.data);
      })
}

Pd: I have printed all the states to see if they're correct, and they are.

Fixed it passing data directly to pacientesService.create() .

From pacientesService.create({paciente}) , to
pacientesService.create({rut:this.state.rut,nombre:this.state.nombre,... })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM