简体   繁体   中英

Unexpected token Indent

I am writting a javascript function in my route which uses some html that is kept in tran.jade file but it gives indent error when i run it on browser. Basically i want to create a function in router that make calculation on students marks and then return a view using express. Please guide me if im doing it the wrong way.

      var express = require('express');
      var router = express.Router();

       /* GET home page. */
       router.get('/', function(req, res, next) {
       res.render('index', { title: 'Express' });
       });
       router.get('/tran', function(req, res, next) {
       var grade = "";  //declare a variable for grade
        var  result="";  //declare a variable for result

        //read the marks
        var engMarks = document.getElementById('txtEnglish').value;
        var kannadaMarks = 
        document.getElementById('txtKannada').value;
        var mathsMarks = document.getElementById('txtMaths').value;
        var scienceMarks = 
        document.getElementById('txtScience').value;

        //calculate the total marks (using double notation technique)
         var totalMarks = engMarks - (- kannadaMarks) - (- mathsMarks) 
        - (- scienceMarks);

        //get the average marks
        var averageMarks = totalMarks / 4;                  

        //find the grade and result using the ternary operator inside 
        the switch statement                  
        switch(


                //usage of ternary operator inside switch

                (averageMarks > 60 && averageMarks <= 100) ? 1 : 
                (averageMarks > 50 && averageMarks < 60) ? 2 : 
                (averageMarks > 40 && averageMarks < 50) ? 3 : 0 
              )

                {
                    case 1 :grade = "A";result="First Class";break;
                    case 2 :grade = "B"; result="Second Class";break;
                    case 3 :grade = "C"; result="Third Class";break;
                    case 0 :grade = "D"; result="Fail";break;
                }


        //display the results   
        document.getElementById('txtStudentName').value = 
        document.getElementById('txtName').value;
        document.getElementById('txtStudentClass').value = 
        document.getElementById('txtClass').value;
        document.getElementById('txtTotalMarks').value = totalMarks;
        document.getElementById('txtAvgMarks').value = averageMarks;
        document.getElementById('txtGrade').value = grade;
        document.getElementById('txtResult').value = result;
        res.render('tran');

        }
        );


        module.exports = router;

You need to understand the difference between server and client:

  • server – the program that responds to HTTP requests from the client, running in the cloud; in your case, this is an Express server built on top of Node.js; the server “serves” HTML code to the client
  • client – the program that sends the HTTP request to the server, usually a web browser like Google Chrome; the client renders the HTML code it gets from the server

In your code example, you are mixing up server and client code and try to run it on the server. You're getting the error “document is not defined” when Node.js executes this line:

document.getElementById('txtStudentName').value = 
    document.getElementById('txtName').value;

That's because document and getElementId access the document object model (DOM) that is created from the HTML that the client renders in the browser. On the server, there is no DOM.

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