【问题标题】:How to get values from GET request in node.js and use it in the POST request如何从 node.js 中的 GET 请求中获取值并在 POST 请求中使用它
【发布时间】:2020-04-06 10:52:01
【问题描述】:

我是 Node.js 和 mysql 的新手。如何从 GET 请求中获取结果并在 node.js 的 POST 中使用它

例如:

var express = require('express');
var app = express();
var mysql =  require('mysql');

var connection =  mysql.createConnection({
        user : 'root',
        host : 'localhost',
        database : '',
        password : ''
});


app.get('/get/pension/:empid', function (req, res) {
  const empid = parseInt(req.params.empid);
  connection.query("SELECT pensionid from pension where empid = ?",[empid], function(error,rows,fields){
    if(!error){
      console.log('connected inside');
      res.send(rows);   
    }
    else {
      console.log('error 1'  + JSON.stringify(error,undefined,2));
    }
  });
});

我想使用 GET 请求中的养老金 ID 并插入到另一个表中。

app.post('/post/vaccination/:petid/:speciesName/:vaccineType/:userName', function (req, res) {
  //should it be request.body? const {pension} =  request.body
  connection.query("INSERT INTO AnotherTable(col1) VALUES(?)",[pension], function(error,rows,fields){
    if(!error){
      console.log('Added');     
    }
    else {
      console.log('error 1' + JSON.stringify(error,undefined,2));
    }
  });
});

非常感谢任何帮助。 谢谢。

【问题讨论】:

    标签: javascript mysql node.js


    【解决方案1】:

    您可以在req 对象中访问发布请求的请求正文。

    要解析 post 正文,您必须添加 body-parser 包来解析请求正文。

    首先安装body-parser 包。

    >$ npm i --save body-parser
    
    

    然后将包添加到您的项目文件中。

    const bodyParser = require('body-parser');
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(bodyParser.json()); // To parse the Json data
    

    然后在req 对象中访问你的身体。

    app.post('/post/vaccination/:petid/:speciesName/:vaccineType/:userName', function (req, res) {
        const {pension} =  req.body;
        connection.query("INSERT INTO AnotherTable(col1) VALUES(?)", [pension], function (error, rows, fields) {
            if (!error) {
                console.log('Added');
            }
            else {
                console.log('error 1' + JSON.stringify(error, undefined, 2));
            }
        });
    });
    

    完整的工作代码:

    var express = require('express');
    var app = express();
    var mysql = require('mysql');
    
    const bodyParser = require('body-parser');
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(bodyParser.json());
    
    var connection = mysql.createConnection({
        user: 'root',
        host: 'localhost',
        database: '',
        password: ''
    });
    
    app.post('/post/vaccination/:petid/:speciesName/:vaccineType/:userName', function (req, res) {
        //should it be request.body? const {pension} =  request.body
        connection.query("INSERT INTO AnotherTable(col1) VALUES(?)", [pension], function (error, rows, fields) {
            if (!error) {
                console.log('Added');
                res.status(201).send("added");
            }
            else {
                console.log('error 1' + JSON.stringify(error, undefined, 2));
                res.status(500).send('error 1' + JSON.stringify(error, undefined, 2));
            }
        });
    });
    

    请注意,您必须在错误和成功的情况下都发送响应。

    【讨论】:

    • body-parser 现已添加到 Express 框架中。 app.use(express.json())
    猜你喜欢
    • 2011-12-16
    • 2019-11-26
    • 1970-01-01
    • 2013-11-01
    • 2016-06-27
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多