Express,React的Body Parser问题。在数据库中发布空值

时间:2019-04-10 00:53:19

标签: reactjs express body-parser

body-parser的中间件问题: 数据被存储为一个对象,该对象随后调用/ api / new来发布到SQL数据库中。

查询本身没有问题,但数据库中的数据为空值。

我的想法:

  1. app.use(express.urlencoded({ 扩展:true })); 设置为true,因为某些值为INT,

  2. 我尝试过JSON.Stringify数据,然后将其发送到POST请求。没用

  3. 还玩着标题

    // const config = { //标头:{ //接受:“ application / json”, //“ accept-language”:“ en_US”, //“ content-type”:“ application / x-www-form-urlencoded” //} //};

axios.post(“ / api / new”,{newEntry},配置...

  1. 我认为问题可能是由于它的router.post位于我的api路由中而不是app.post

    ```
         var newEntry = {
              First_Name: FirstName.toUpperCase(),
              Last_Name: LastName.toUpperCase(),
              Employ_ID: parseInt(EmployID),
              Problem: checkedValue,
              PhoneNumber: phoneNumber,
              Provider: domain
            };
            var stringNewEntry = JSON.stringify(newEntry);
    
            console.log("Right before Post");
            console.log(newEntry);
    
            axios
              .post(
                "/api/new",
               {newEntry},
                {
                  timeout: 10000
                }
              )
              .catch(error => {
                if (error.code === "ECONNABORTED") return "timeout";
              })
              .then(res => {
                console.log(newEntry);
                if (res === "timeout") {
                  console.log("Please Try Again");
                } else {
                  console.log("SUCCESS!");
                }
              });
          }
    ````
    

    服务器文件

        const express = require("express");
        const app = express();
        var bodyParser = require('body-parser')
    
        const PORT = process.env.PORT || 3001;
        var routes = require("./routes/api-routes");
        // Define middleware here
    
        app.use(express.json());
        app.use(bodyParser.json());
        app.use(express.urlencoded({
          extended: true
        }));
    
        // Serve up static assets (usually on heroku)
        if (process.env.NODE_ENV === "production") {
          app.use(express.static("client/build"));
        }
        // Add routes, both API and view
        app.use(routes);
    
        // Start the API server
        app.listen(PORT, function() {
          console.log(`  ==> API Server now listening on PORT ${PORT}!`);
        });
    

api路由


    router.route("/api/new").post(function(req, res) {
  console.log("Data:");

  console.log(req.body.First_Name);

  var dbQuery =
    "INSERT INTO KioskInfo (First_Name,Last_name,Employ_ID,Problem,PhoneNumber,Provider) VALUES (?,?,?,?,?,?)";
  var dbQueryPermanent =
    "INSERT INTO KioskData (First_Name,Last_name,Employ_ID,Problem) VALUES (?,?,?,?)";
  var IgnoreQuery =
    "INSERT IGNORE INTO KioskInfo (First_Name,Last_name,Employ_ID,Problem,PhoneNumber,Provider) VALUES (?,?,?,?,?,?)";

  connection.query(
    dbQuery,
    [
      req.body.First_Name,
      req.body.Last_Name,
      req.body.Employ_ID,
      req.body.Problem,
      req.body.PhoneNumber,
      req.body.Provider
    ],
    function(err, result) {
      if (err) {
        connection.query(
          IgnoreQuery,
          [
            req.body.First_Name,
            req.body.Last_Name,
            req.body.Employ_ID,
            req.body.Problem,
            req.body.PhoneNumber,
            req.body.Provider
          ],
          function(IgnErr, IgnResult) {
            if (IgnErr) {
              throw IgnErr;
            } else {
              console.log("Duplicate Entry Ignored");
            }
          }
        );
      }
      //Learn to specify DUP ERR;
      console.log("Successfully Saved Into KioskInfo");
    }
  );

  connection.query(
    dbQueryPermanent,
    [
      req.body.First_Name,
      req.body.Last_Name,
      req.body.Employ_ID,
      req.body.Problem
    ],
    function(err, result) {
      if (err) throw err;
      console.log("Successfully Saved Into KioskData");
      res.end();
    }
  );
});

0 个答案:

没有答案