简体   繁体   中英

React-native getting a response after making a post request is not working

I am trying to send back a response after making a successful post request in react-native, but the response does not arrive properly.

router.route("/addUser").post((req, res) => {
 let name= req.body.name;
  connection.query(
    `INSERT INTO users (id,name) VALUES (NULL,${name})`,

    function (error, results) {
      console.log("REAL RESULTS: " + JSON.stringify(results));
      if (error) throw error;

      res.send("Inserted successfully"); 
    }
  );
});

Then, when I try to receive the request and print its content (which is expected to be: Inserted Successfully ) I get:

{"type":"default","status":200,"ok":true,"headers":{"map":{"x-powered-by":"Express","content-length":"21","connection":"keep-alive","content-type":"text/html; charset=utf-8","etag":"W/"15-fAIFznmhviiIlY1HeyBcwGlWsmo"","date":"Wed, 12 Aug 2020 12:35:59 GMT","cache-control":"public, max-age=0"}},"url":"http://192.168.1.9:3000/database/addUser","bodyUsed":false,"_bodyInit":{"_data":{"size":21,"offset":0,"blobId":"aeb788fe-9bab-4641-a0a3-25e5df4c8890","__collector":{}}},"_bodyBlob":{"_data":{"size":21,"offset":0,"blobId":"aeb788fe-9bab-4641-a0a3-25e5df4c8890","__collector":{}}}}

This is my code for making the request:

fetch("http://192.168.1.9:3000/database/addUser", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      name: "Some name",
    }),
  })
    .then((res) => {
      console.log(JSON.stringify(res));

Try

fetch("http://192.168.1.9:3000/database/addUser", {
    method: "POST",
    mode: "no-cors",
    cache: "no-cache", 
    credentials: "same-origin", 
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      name: "Some name",
    }),
  })
  .then(response => response.json())
  .then(data => console.log(data));
    

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