I'm currently working on a simple Node.JS app that communicates with mysql database.
The app gets data from the database and puts it in a table using app.get and it works fine.
The problem I am having is that req.body is not returning anything when using app.post. The app will submit to the database, but because req.body is not returning anything, the values are null. It will post actual values if I swap the req.body variables with hardcoded variables e.g. "12320".
I have therefore isolated the problem to post requests which use req.body.
If I console log req.body it is empty.
I have read just about every stackoverflow question on this and have not been able to find a solution.
I have tried reinstalling body-parser, express, morgan, and mysql.
I have tried recreating the app in a fresh directory and installing the modules again.
I have tried renaming the variables incase that was having some effect (e.g. name="test", req.body.test)
But all of this was to no avail.
<form id="submit_missing" action="/submit_quote" method="POST" enctype="multipart/form-data">
<input type="text" id="submit_quote_id" name="submit_quote_id" placeholder="Quote ID">
<input type="text" id="submit_priority" name="submit_priority" placeholder="Priority" list="priority_list">
<input type="text" id="submit_customer" name="submit_customer" placeholder="Customer">
<input type="text" name="submit_who" id="submit_who" placeholder="Who's On it" list="purchasing">
<input type="text" id="submit_account_manager" name="submit_account_manager" placeholder="Account Manager" list="account_managers">
<button id="submit_quote">SUBMIT</button>
<form>
// server.js
const express = require('express')
const app = express()
const morgan = require('morgan')
const mysql = require('mysql')
const bodyParser = require('body-parser')
app.use(bodyParser.json({ type: '*' }))
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static('./public'))
app.use(express.static('./files'))
const user = ***
const pass = ***
function getBoardConnection() {
return mysql.createConnection({
host: "localhost",
port: "3306",
user: user,
password: pass,
database: "board"
})
}
app.get('/get_board_quotes', (req, res) => {
const con = getBoardConnection()
const queryString = "SELECT * FROM board_quotes WHERE quote_complete = '0'"
con.query(queryString,(err, rows, fields) => {
if (err) {
console.log("Failed to query for /get_board_quotes : " + err)
}
console.log("Getting data from database for /get_board_quotes")
res.json(rows)
})
})
app.post('/submit_quote/', (req, res) => {
console.log(req.body)
quote_id = req.body.submit_quote_id
priority = req.body.submit_priority
customer = req.body.submit_customer
who_quoted = req.body.submit_who
account_manager = req.body.submit_account_manager
type = ""
queryString = "INSERT INTO board_quotes (quote_id, priority, customer, who_quoted, account_manager, quote_type) \
VALUES (?, ?, ?, ?, ?, ?)"
getBoardConnection().query(queryString, [quote_id, priority, customer, who_quoted, account_manager, type], (err, results, field) => {
if (err) {
console.log("Failed to insert new board order " + err)
return res.redirect('/board/quotes.html')
}
console.log("Inserted a new board with id ")
res.redirect('/board/quotes.html')
})
})
app.listen(6565, () => {
console.log("Server is running")
})
Output
Server is running
Getting data from database for /get_board_quotes
{}
Failed to insert new board order Error: ER_BAD_NULL_ERROR: Column 'quote_id' cannot be null
{} is the output for console.log(req.body)
Any help would be greatly appreciated.