简体   繁体   English

意外的标记缩进

[英]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.我在我的路线中写了一个 javascript function,它使用了一些保存在 tran.jade 文件中的 html,但是当我在浏览器上运行它时会出现缩进错误。 Basically i want to create a function in router that make calculation on students marks and then return a view using express.基本上我想在路由器中创建一个 function 来计算学生的分数,然后使用 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;服务器——响应来自客户端的 HTTP 请求的程序,在云中运行; in your case, this is an Express server built on top of Node.js;在您的情况下,这是建立在 Node.js 之上的 Express 服务器; the server “serves” HTML code to the client服务器向客户端“提供”HTML 代码
  • client – the program that sends the HTTP request to the server, usually a web browser like Google Chrome;客户端——向服务器发送 HTTP 请求的程序,通常是 web 浏览器,例如 Google Chrome; the client renders the HTML code it gets from the server客户端呈现从服务器获取的 HTML 代码

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:当 Node.js 执行此行时,您将收到错误“文档未定义”:

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.这是因为documentgetElementId访问的是从客户端在浏览器中呈现的 HTML 创建的文档 object model (DOM)。 On the server, there is no DOM.在服务器上,没有 DOM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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