Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

69
Vistas
How To make a post request from react to node/express api

All i need to do is to send my state of invoice details to database through post request but im having troubles doing it is this the right way to do it or am i missing something post function works fine if the query is only a string so the only problem is reading the body params

const postInvoices = () => {
    const URL = "http://localhost:8000/api/InvSave";
    axios
      .post(URL,InvDet)
      .then((response) => {
        console.log("DATA : ",response);
      })
      .catch((error) => {
        console.log(error);
      });  
  };

im sending the state on click

in my api i wrote :

router.route('/InvSave').post((request,response)=>{
try{
   const invoices =  request.body

   dboperations.PostInvoices(invoices).then(result => {
      response.status(201).json("api results :",result);
   })
}catch(err){
   console.error(err)
}
})
const PostInvoices = async (invoices) => {
  try {
    let pool = await sql.connect(configInsert);
    console.log("invoices code",CODE_ART)
    const q =
      "insert into Packingdetails values('1','"+
      invoices.CODE_ART +
      "','" +
      invoices.a_code +
      "','" +
      invoices.DESC_ART +
      "','" +
        invoices.TotalPc +
      "','" +
        invoices.date+    
      "')";
      console.log("query : "+q)
    let invs = await pool.query(q);
    console.log("saved");
    return invs.recordsets;
  } catch (err) {
    console.log("POSTINV : ", err.message);
  }
};
7 months ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

make sure you are using some kind of body-parser to parse your request console.log(invoices) to check if you are getting the correct invoices in request body

7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.