简体   繁体   English

如何从反应到节点/表达 api 发出发布请求

[英]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我需要做的就是通过发布请求将发票详细信息的 state 发送到数据库,但我遇到了麻烦,这是正确的方法还是我错过了 function 后的内容,如果查询只是一个字符串,那么唯一的问题是阅读身体参数

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我点击发送 state

in my api i wrote:在我的 api 我写道:

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);
  }
};

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确保您使用某种正文解析器来解析您的请求console.log(invoices)以检查您是否在请求正文中获得了正确的发票

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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