【发布时间】: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