【发布时间】:2020-01-12 04:00:07
【问题描述】:
我正在使用 Express.js 和 mysql 包来创建我的 api,但我无法发帖。
这是我目前的代码:
const express = require('express');
const mysql = require('mysql');
const config = mysql.createConnection({
host: theHost,
port: thePort,
user: theUser,
password: thePass,
database: theDB,
});
const app = express();
config.connect(function(err){
if(!err) {
console.log("Success");
} else {
console.log("Error trying to connect");
}
});
app.get("/api/InternalAccess", function(req, res){
config.query('SELECT * from InternalAccess', (error, result) => {
if (error) throw error;
res.send(result);
});
});
app.post("/api/internalAccess", function(req, res){
var info = { User: req.body.User, Password: req.body.Password, CreationDate: req.body.CreationDate };
config.query('INSERT INTO InternalAccess SET ?', info, (error, result) => {
if (error) throw error;
res.send(result);
});
});
app.listen(3000);
我对 get 没有任何问题,它工作正常,但要从邮递员发帖,我收到错误:“无法读取未定义的属性“用户””。我在逃避什么吗?我真的是使用 mysql 包的新手。
我的数据库是 MySQL Workbench,正如我所说,我正在使用 Node.js、Express.js 和 mysql 包。
希望你能帮助我。提前致谢
【问题讨论】:
标签: mysql node.js express mysql-workbench