首页 > 解决方案 > 如何使用 express.js 从 pug 中的文本输入中获取值?

问题描述

在一项任务中,我想从文本框中获取文本的值并将其与数组进行比较,并检查我向用户显示的问题的答案是否正确。问题也在数组内。如果答案是正确的,我想添加分数变量并将下一个问题传递给用户,最后显示当前分数。这是我写的代码我的 index.pug 是

doctype doctype html
html(lang="en")
    head
        meta(charset="UTF-8")
        meta(http-equiv="X-UA-Compatible", content="IE=edge")
        meta(name="viewport", content="width=device-width, initial-scale=1.0")
        title Number quiz lab
    body 
        h1 The number quiz 
        form(name="add-estimation", method="post")
            p   Your current score is #{score}
            p   Guess the next number in the sequence 
            p   #{question}
            label Your answer: 
            input(
                type='text'
                name='answer'
            )
            br
            input(type="submit", value="Submit")

和我的 index.js

const express = require('express');
const path = require('path');

const app = express();

//load view engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.get('/', function(req, res){
    const questions = [
        "3, 1, 4, 1, 5",  
        "1, 1, 2, 3, 5",
        "1, 4, 9, 16, 25",
        "2, 3, 5, 7, 11",
        "1, 2, 4, 8, 16",
    ]

    const answers = [9, 8, 36, 13, 32];

    for(var i = 0; i < questions.length; i++){
        res.render('index', {
            question: questions[i]
        });
        if(answer == answers[i]){
            // console.log('your answer is correct');
            score++;
            alert(score);
        }
    }
});

app.post('/', function (req, res) {
    // console.log(req.body.score);
    const questions = [
        "3, 1, 4, 1, 5",
        "1, 1, 2, 3, 5",
        "1, 4, 9, 16, 25",
        "2, 3, 5, 7, 11",
        "1, 2, 4, 8, 16",
    ]

    const answers = [9, 8, 36, 13, 32];
    var myanswer = req.body.answer;
    console.log(myanswer);
    res.render('index');
});

app.listen(3000, function(req, res){
    console.log('server started on port 3000');
});

我的错误如下所示

TypeError: Cannot read property 'answer' of undefined
at C:\Users\student\nodeprojects\numberquizlab\index.js:44:29
at Layer.handle [as handle_request] (C:\Users\student\nodeprojects\numberquizlab\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\student\nodeprojects\numberquizlab\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\student\nodeprojects\numberquizlab\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\student\nodeprojects\numberquizlab\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\student\nodeprojects\numberquizlab\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\student\nodeprojects\numberquizlab\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\student\nodeprojects\numberquizlab\node_modules\express\lib\router\index.js:275:10)
at expressInit (C:\Users\student\nodeprojects\numberquizlab\node_modules\express\lib\middleware\init.js:40:5)
at Layer.handle [as handle_request] (C:\Users\student\nodeprojects\numberquizlab\node_modules\express\lib\router\layer.js:95:5)

标签: expresspug

解决方案


推荐阅读