I am working on a project for a pretend library database and website interface. Right now only 2 of the 4 new loan form inputs are being passed to the req.body. All of the items have a name attribute but only the input fields are going through and the select/option fields are not. Any help is greatly appreciated! Project information and link to all other project files are on GitHub. Here is the link to GitHub
const express = require("express"), app = express(), path = require("path"), cookieParser = require("cookie-parser"), bodyParser = require("body-parser"), routes = require("./routes/index"); //Setting Up // Pug as the view engine, bodyParser, and cookieParser app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'pug'); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use(cookieParser()); app.use('/static', express.static('public')); const express = require("express"), router = express.Router(); Books = require("../models").Books; Loans = require("../models").loans; Patrons = require("../models").patrons; router.post('/newloan', (req, res, next) => { Loans.create(req.body).then((loan)=>{ console.log('/////////////////////////////////////////'); console.log(req.body); console.log('/////////////////////////////////////////'); res.redirect('/allloans'); }); });
extends ../layout block content h1= title form( method="POST" action="/newloan") p label(for='book_id') Book select#book_id each book in books option(value=book.id name="book_id")= book.title p label(for='patron_id') Patron select#patron_id each patron in patrons option(value=patron.id name="patron_id") #{patron.first_name} #{patron.last_name} p label(for='loaned_on') Loaned on: input#loaned_on(type='date' name="loaned_on" value=loan.loaned_on) p label(for='return_by') Return by: input#return_by(type='date' name="return_by" value=loan.return_by) p input(type='submit' value='Create New Loan')
You did not provided name for select box, following may help you
extends ../layout
block content
h1= title
form( method="POST" action="/newloan")
p
label(for='book_id') Book
select#book_id(name="book_id")
each book in books
option(value=book.id)= book.title
p
label(for='patron_id') Patron
select#patron_id(name="patron_id")
each patron in patrons
option(value=patron.id) #{patron.first_name} #{patron.last_name}
p
label(for='loaned_on') Loaned on:
input#loaned_on(type='date' name="loaned_on" value=loan.loaned_on)
p
label(for='return_by') Return by:
input#return_by(type='date' name="return_by" value=loan.return_by)
p
input(type='submit' value='Create New Loan')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.