express - 如何使用 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)
解决方案
推荐阅读
- hadoop - Hadoop 2.7.7,无法使用端口 8088 打开资源管理器 Web
- java - 上下文初始化期间遇到异常
- haskell - 每个元素与前一个元素最多相差 1 的随机列表
- sql - SQL Server 2008 R2 - sp_send_dbmail
- css - CSS媒体查询选择性地不起作用
- r - ShinyR:在数据库中插入用户输入以供进一步使用
- ios - didSelectRowAt(indexPath:) 在平移手势后返回错误的行
- jquery - 为什么切换 div 标签在我的代码中不起作用?
- reactjs - 使用redux-form验证动态生成的表单并重新验证?
- excel - VBA Excel循环写入数据;访问 VBA 循环读取数据 - VBA.CreateObject 错误