简体   繁体   中英

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.

Button#5 is for updating the monthly apt fee. 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 .

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.

    // 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.

    <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 .

UPD: The issue is here

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

You need to have name field here. The name field defines the name of that value in the body of the request.

Try this.

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM