[英]I can't access a user input in node.js
I am developing an apt management app.我正在开发一个合适的管理应用程序。
I managed to get all functions to work but I am having trouble with the last one.我设法使所有功能正常工作,但最后一个功能出现问题。
Basically, user selects one of the 5 update options from the db update menu page by clicking one of the submit buttons numbered from 1 to 5.基本上,用户通过单击编号为 1 到 5 的提交按钮之一,从数据库更新菜单页面中选择 5 个更新选项之一。
Button#5 is for updating the monthly apt fee.按钮#5 用于更新每月 apt 费用。 User enters the new monthly fee and clicks the update button.
用户输入新的月费并点击更新按钮。
But no matter what I do, I can't access the user input in req.body.newFee
.但无论我做什么,我都无法访问
req.body.newFee
的用户输入。
When I display it at the console, node displays it as undefined.当我在控制台上显示它时,节点将其显示为未定义。 Can someone tell me what I am doing wrong?
有人能告诉我我做错了什么吗?
Below is my main server.js file.下面是我的主要 server.js 文件。
// Send login page file to the client.
app.get('/loginpg', function(req, res) {
res.sendFile("D:/Behrans-files/Web-projects/havuzlusite/loginpg.html");
});
app.post('/server', (req, res) => { //Post request to receive user login data that was posted to server.js by login form.
var usrname = req.body.isim;
var usrpwd = req.body.sifre;
if (usrname && usrpwd) { //Check if user has entered name and password in the login form.
if (usrname == 'Yonetim' && usrpwd == "admin") { //If building management name and pwd entered,
res.render('dbupdmenupg'); //Display db update menu page.
//Route to handle db update menu page.
app.post('/dbupdmenupg/:btnno', (req, res) => { // Get the number of clicked button.
console.log("newFee1: ", req.body.newFee);
// Route to handle apt fee payment - If button#1 was clicked.
if (req.params.btnno == 1) {
res.render('usrpmtpg'); //Display user apt fee payment page.
app.post('/', (req, res) => { //Post request to access payment month and payment amount inputs from user.
var usrname = req.body.usrname;
var pmtmnth = req.body.pmt_mnth;
var pmtamt = req.body.pmt_amt;
queryUsrName(usrname, function(response) { //Pass usrname and call function to see if the user is in db.
console.log('status_flg: ', response);
if (response == 'Found') { //If response has no error message, call function to update user payment data in db.
updateUsrPmtData(usrname, pmtmnth, pmtamt, function(response) { //Call function to update user apt fee payment data in db.
alert(response); //Display db update status message from called function.
res.render('usrpmtpg');
});
} else if (response == 'Not found')
alert('İsim veri tabanında bulunamadı. Ana sayfaya dönmek için lütfen Ana sayfa butonuna tıklayınız!'); //If response has error message, display error message.
else
alert('Site sakini ismi veri tabanında aranırken sorun oluştu.');
})
})
}
// Route to handle deletion of existing resident user - If button#2 was clicked.
if (req.params.btnno == 2) {
res.render('deluserpg');
app.post('/', (req,res) => {
var usrname = req.body.usrname;
queryUsrName(usrname, function(response) { //Pass usrname and call function to see if the user is in db.
if (response == 'Found') { //If response has no error message, it means user is in db, call function to delete it.
deleteUser(usrname, function(response) { // Pass username input data as parameter to call deleteuser function.
alert(response); //Display db delete status message from called function.
res.render('dbupdmenupg');
})
} else if (response == 'Not found') {
alert('İsim veri tabanında bulunamadı. Lütfen sistemde mevcut bir isim girin.'); //If response has error message, display error message.
res.render('deluserpg');
} else
alert('Site sakini ismi veri tabanında aranırken sorun oluştu.');
})
})
}
// Route to handle addition of new resident user - If button#3 was clicked.
if (req.params.btnno == 3) {
res.render("adduserpg");
app.post('/', (req,res) => {
var usrname = req.body.newname;
queryUsrName(usrname, function(response) { //Pass usrname and call function to see if the user is in db.
if (response == 'Found') {
alert('Isim veri tabaninda mevcut, tekrar eklenemez. Lütfen sistemde olmayan bir isim girin. '); //If response has error message, display error message.
} else {
//If response has error message, it means user is not in db, call function to add it.
addUser(req.body.newname, req.body.newpwd, req.body.newblokno, req.body.newdaireno, req.body.newaidat, function(response) { //Pass input data as parms to call addUser funcn.
alert(response);
})
}
res.render('adduserpg');
})
})
}
// Route to handle reseting residents for the new year - If button#4 was clicked.
if (req.params.btnno == 4) {
newyearReset(function(response) {
alert(response);
})
}
**// Route to handle updating apt monthly fee - If button#5 was clicked.
if (req.params.btnno == 5) {
res.render('updfeepg');
app.post('/updfeepg', (req,res) => {
newFee = req.body.newFee;
console.log("newFee: ", newFee);
if (newFee) {
aptfeeUpdate(newFee, function(response) {
alert(response);
})
}
})
res.end();**
// res.redirect("/dbupdmenupg");
}
})
} else { //If a resident user name and pwd entered, call function to query on name/pwd in database.
queryUsrNamePwd(usrname, usrpwd, function(rows) {
if (rows) { // If user name/pwd match db,
res.render('userdatapg', {rows}); // Display resident data.
} else
res.redirect('/loginpg');
})
}
} else //if no user name and pwd entered, display message
alert('Lütfen isim ve şifre giriniz!');
//res.end();
});
Below is the html form file updfeepg.html.下面是 html 表单文件 updfeepg.html。
<body>
<! Create database update menu form >
<form class="updfee" action="/updfeepg" method="post">
<p class="parag" >Lütfen yeni aylık aidatı giriniz.<input type="number" class="newFee" id="newFee" max="999"></p>
<br>
<button type="submit" class="updfeebtn" name="updfeebtn" id="updfeebtn" >Aidatı güncelle</button>
<a href="http://localhost:3000" type="button" class='homepg-btn'>Ana sayfa</a>
</form>
</body>
You need to parse the body firstly before you could access it.您需要先解析正文,然后才能访问它。
For that you can use multer .为此,您可以使用multer 。
UPD: The issue is here UPD:问题就在这里
<input type="number" class="newFee" id="newFee" max="999">
You need to have name
field here.您需要在此处设置
name
字段。 The name
field defines the name of that value in the body of the request. name
字段定义请求正文中该值的名称。
Try this.尝试这个。
<input type="number" class="newFee" id="newFee" name="newFee" max="999">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.