vendredi 2 avril 2021

Node JS Error: Problem fetching data from frontend

I am working on a elective management system and for that I was using Node JS. In the HTML page there is a field table which is this [IMAGE] : Click Here

The code for the table part is :

<div class="container1">
    <form id="contact" action="/admin-elective-change" method="post">
    <table id="contact" width="100%" cellspacing="2" cellpadding="2" border="5">
    <tr>
    <th>Elective ID</th> 
    <th>Elective Name</th>
    <th>Elective Code</th>
    <th>Semester</th>
    <th>Faculty</th>
    <th>Minimum GPA</th>
     
    </tr>

    <%
        if(userData.length!=0){
        var i=1;
        var j =1;
        userData.forEach(function(data){
    %>
     
    <tr>
     
    <!--<td contentEditable="true" align="center" name =<%=data.courseId %> ><%=data.courseId %></td>
    <td contentEditable="true" align="center" name =<%=data.courseId %> ><%=data.courseName %></td>
    <td contentEditable="true" align="center" name =<%=data.courseId %> ><%=data.courseCode %></td>
    <td contentEditable="true" align="center" name =<%=data.courseId %> ><%=data.semester %></td>
    <td contentEditable="true" align="center" name =<%=data.courseId %> ><%=data.faculty %></td>
    <td contentEditable="true" align="center" name =<%=data.courseId %> ><%=data.minGPA %></td>
    </tr>-->

    <td contentEditable="true" align="center" name =<%=j %> ><%=data.courseId %></td>
    <% j++; %>
    <td contentEditable="true" align="center" name =<%=j %> ><%=data.courseName %></td>
    <% j++; %>
    <td contentEditable="true" align="center" name =<%=j %> ><%=data.courseCode %></td>
    <% j++; %>
    <td contentEditable="true" align="center" name =<%=j %> ><%=data.semester %></td>
    <% j++; %>
    <td contentEditable="true" align="center" name =<%=j %> ><%=data.faculty %></td>
    <% j++; %>
    <td contentEditable="true" align="center" name =<%=j %> ><%=data.minGPA %></td>
    <% j++; %>
    
    </tr>

    <% i++; }) } %>
    </table>
    <button name="edit-elective" type="submit" id="contact-submit" data-submit="...Sending">Make Changes</button>
    </form>
     </div>

The code for the route part is :

var express = require('express');
var router = express.Router();
var db=require('../database');
/* GET users listing. */
router.get('/adminelective', function(req, res, next) {
    var sql = 'SELECT * FROM electives';
    db.query(sql, function(err,data,fields){
        if(err) throw err;
        res.render('admin-elective', {userData:data})
    })
});
router.post('/admin-elective-change', function(req, res){
    var sql = 'SELECT * FROM electives';
    db.query(sql, function(err,result){
        console.log("Total table rows : "+ result.length );
        
        console.log("Entry in req field : "+ req.body.2);  /*ERROR LINE */
    })
    
});
module.exports = router;

Now what I am doing is for every entry in the table I am increasing the count of variable j by one and assigning that value of j to each field in a row and repeating the same for continuous increase in value of j for next row and so on.

Finally on click of the submit button in the backend I am for testing purposes printing the value written in the textfield with name 2 but it gives me an error while running the node server which is as follows : ERROR

Objective : My objective with this is to iterate over the entire table in the backend. Push the elements of a row in array and insert it into a database and continue to do so until it is finished.

Please explain me where I am going wrong and if there is an alternate way of doing then please help me with that.




Aucun commentaire:

Enregistrer un commentaire