簡體   English   中英

我無法訪問 node.js 中的用戶輸入

[英]I can't access a user input in node.js

我正在開發一個合適的管理應用程序。

我設法使所有功能正常工作,但最后一個功能出現問題。

基本上,用戶通過單擊編號為 1 到 5 的提交按鈕之一,從數據庫更新菜單頁面中選擇 5 個更新選項之一。

按鈕#5 用於更新每月 apt 費用。 用戶輸入新的月費並點擊更新按鈕。

但無論我做什么,我都無法訪問req.body.newFee的用戶輸入。

當我在控制台上顯示它時,節點將其顯示為未定義。 有人能告訴我我做錯了什么嗎?

下面是我的主要 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();    
    }); 

下面是 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>

您需要先解析正文,然后才能訪問它。

為此,您可以使用multer

UPD:問題就在這里

<input type="number" class="newFee" id="newFee" max="999">

您需要在此處設置name字段。 name字段定義請求正文中該值的名稱。

嘗試這個。

<input type="number" class="newFee" id="newFee" name="newFee" max="999">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM