0

I got undefined values when I send these values to the api. Note : I use postman and it works good . this is my code on react app

 submithandler=(e)=>{
      e.preventDefault();
     axios.post('http://localhost:8000/api/addsickers',
     JSON.stringify({
         ID:"123456789",
         Blod:"22334445",
         Allergic:"6677788",
         Chronic:"3445566"
        })
     )
       .then(response=>{
           alert(response);
       })
       .catch(err=>{
           alert("catch"+err);
       });
  }

and this is the api code :

 var person = req.body;
    /* const schema = joi.object().keys({
         ID: joi.string().min(5).max(50).required(),
         Blod: joi.string().required(),
         Allergic: joi.string(),
         Chronic: joi.string(),
     });*/
    // validate data
    // if (!joi.validate(person, schema)) {
    con.connect();
    con.query('INSERT INTO `sick`(`ID`, `Blod`, `Allergic`, `Chronic`) VALUES ("' + person.ID + '","' + person.Blod + '","' + person.Allergic + '","' + person.Chronic + '")', function(err, rows, fields) {

            if (err) {
                // res.send(toString(err));
                console.log(err);
            }
            // res.send("registred succefully");
        })
        // } else {
        //   res.send("please validate your data");
        //}
        //ID + Blod + allergic + chronic +
    con.end()
    console.log(req.body);

Note : the console.log(person) shows me a json array that means data has transfared . but in insert it s defined , for example "person.ID" is undefined . the question is how to get non undefined values from this json array . cause I tried some methods and does not work for me . thanks in advance.

azdeviz
  • 597
  • 2
  • 9
  • 20
  • please provide server and client code, maybe this can help https://stackoverflow.com/questions/16134378/node-js-and-mysql-callback-query-in-query-callback – Nikita May 09 '20 at 17:48
  • thanks for your response , I have edited the error , the first error I have mentioned was related to get method and the app crashed I have solved it , but the error that related to post method was this error that I have added , please look at this again. – azdeviz May 09 '20 at 23:07
  • Maybe you are not using the body-parser. But we can only confirm if you put the complete code from your server, where you declared your module express. We can confirm if you do a `typeof req.body`. – Danizavtz May 10 '20 at 01:55
  • I found the problem exactly at the post method in client side , the type of body data when posted to the api it returns a long string , and when I use postman it returns a json array , I have asked complete question here https://stackoverflow.com/questions/61705905/json-post-method-format/61706284#61706284 – azdeviz May 10 '20 at 02:22
  • Not recommended to creae multiple questions for one issue , when need you update your quesion, if you want separate update, just use header ###UPDATE 1/2/3/// – Nikita May 10 '20 at 07:16
  • First of all you have to ensure that you are sending the correct format from the client, open network tab, in Chrome browser f12 and trigger the request. THen in network tab you shoud the request and you can check what really was sent. Then we can understand it is bad formatting on client, or bad parsing on server :) – Nikita May 10 '20 at 07:18
  • check in postman headers that were used to send the request, possibly you have to duplicate them – Nikita May 10 '20 at 07:19

1 Answers1

0

it was fixed by one of the following way but I prefer the first one :

first you can just add cors to the api

npm install cors 

const cors =require('cors')

app.use(cors())

router.post('path',cors(),(req,res).....

the second is adding :

JSON.parse(req.body)

to read data in json forma.

azdeviz
  • 597
  • 2
  • 9
  • 20