Javascript 表单post请求期间未定义变量
我需要通过html表单发送值,并在console.log中接收。但一旦我发送了数据,错误就会弹出“TypeError:无法读取未定义的属性'fName'”,即使我已经在表单名称中声明了它 signin.html:Javascript 表单post请求期间未定义变量,javascript,node.js,express,Javascript,Node.js,Express,我需要通过html表单发送值,并在console.log中接收。但一旦我发送了数据,错误就会弹出“TypeError:无法读取未定义的属性'fName'”,即使我已经在表单名称中声明了它 signin.html: <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewpo
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
<meta name="generator" content="Hugo 0.80.0">
<title>Signin Template · Bootstrap v5.0</title>
<link rel="canonical" href="https://getbootstrap.com/docs/5.0/examples/sign-in/">
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Favicons -->
<link rel="apple-touch-icon" href="/docs/5.0/assets/img/favicons/apple-touch-icon.png" sizes="180x180">
<link rel="icon" href="/docs/5.0/assets/img/favicons/favicon-32x32.png" sizes="32x32" type="image/png">
<link rel="icon" href="/docs/5.0/assets/img/favicons/favicon-16x16.png" sizes="16x16" type="image/png">
<link rel="manifest" href="/docs/5.0/assets/img/favicons/manifest.json">
<link rel="mask-icon" href="/docs/5.0/assets/img/favicons/safari-pinned-tab.svg" color="#7952b3">
<link rel="icon" href="/docs/5.0/assets/img/favicons/favicon.ico">
<meta name="theme-color" content="#7952b3">
<style>
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
@media (min-width: 768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem;
}
}
</style>
<!-- Custom styles for this template -->
<link href="css/styles.css" rel="stylesheet">
</head>
<body class="text-center">
<main class="form-signin">
<form action="/" method="POST">
<img class="mb-4" src="images/logo.jpg" alt="" width="72" height="57">
<h1 class="h3 mb-3 fw-normal">Sign Up Here</h1>
<input type="text" name ="fName" class="form-control top" placeholder="First Name" required autofocus>
<input type="text" name ="lName" class="form-control middle" placeholder="Last Name" required>
<input type="email" name ="email" class="form-control bottom" placeholder="Email" required>
<button class="w-100 btn btn-lg btn-primary signinbutton" type="submit">Sign Up!</button>
<p class="mt-5 mb-3 text-muted">© The ThunderStorm</p>
</form>
</main>
</body>
</html>
//jshint esversion:6
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const app = express();
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));
app.get('/', function(req, res){
res.sendFile(__dirname + "/signup.html");
});
app.post("/",function(req,res){
var firstName = res.body.fName;
var lastName = res.body.lName;
var email = res.body.email;
console.log(firstName,lastName,email);
});
app.listen(3000,function(){
console.log("Started listening on port 3000");
});
错误日志:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
<meta name="generator" content="Hugo 0.80.0">
<title>Signin Template · Bootstrap v5.0</title>
<link rel="canonical" href="https://getbootstrap.com/docs/5.0/examples/sign-in/">
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Favicons -->
<link rel="apple-touch-icon" href="/docs/5.0/assets/img/favicons/apple-touch-icon.png" sizes="180x180">
<link rel="icon" href="/docs/5.0/assets/img/favicons/favicon-32x32.png" sizes="32x32" type="image/png">
<link rel="icon" href="/docs/5.0/assets/img/favicons/favicon-16x16.png" sizes="16x16" type="image/png">
<link rel="manifest" href="/docs/5.0/assets/img/favicons/manifest.json">
<link rel="mask-icon" href="/docs/5.0/assets/img/favicons/safari-pinned-tab.svg" color="#7952b3">
<link rel="icon" href="/docs/5.0/assets/img/favicons/favicon.ico">
<meta name="theme-color" content="#7952b3">
<style>
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
@media (min-width: 768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem;
}
}
</style>
<!-- Custom styles for this template -->
<link href="css/styles.css" rel="stylesheet">
</head>
<body class="text-center">
<main class="form-signin">
<form action="/" method="POST">
<img class="mb-4" src="images/logo.jpg" alt="" width="72" height="57">
<h1 class="h3 mb-3 fw-normal">Sign Up Here</h1>
<input type="text" name ="fName" class="form-control top" placeholder="First Name" required autofocus>
<input type="text" name ="lName" class="form-control middle" placeholder="Last Name" required>
<input type="email" name ="email" class="form-control bottom" placeholder="Email" required>
<button class="w-100 btn btn-lg btn-primary signinbutton" type="submit">Sign Up!</button>
<p class="mt-5 mb-3 text-muted">© The ThunderStorm</p>
</form>
</main>
</body>
</html>
//jshint esversion:6
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const app = express();
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));
app.get('/', function(req, res){
res.sendFile(__dirname + "/signup.html");
});
app.post("/",function(req,res){
var firstName = res.body.fName;
var lastName = res.body.lName;
var email = res.body.email;
console.log(firstName,lastName,email);
});
app.listen(3000,function(){
console.log("Started listening on port 3000");
});
TypeError:无法读取未定义的属性“fName”
在G:\WebDevelopmentFolder\Newsletter Signup\app.js:18:28
在Layer.handle[作为handle\u请求](G:\WebDevelopmentFolder\Newsletter Signup\node\u modules\express\lib\router\Layer.js:95:5)
下一步(G:\WebDevelopmentFolder\Newsletter Signup\node\u modules\express\lib\router\route.js:137:13)
在Route.dispatch(G:\WebDevelopmentFolder\Newsletter Signup\node\u modules\express\lib\router\Route.js:112:3)
在Layer.handle[作为handle\u请求](G:\WebDevelopmentFolder\Newsletter Signup\node\u modules\express\lib\router\Layer.js:95:5)
位于G:\WebDevelopmentFolder\Newsletter Signup\node\u modules\express\lib\router\index.js:281:22
位于Function.process_参数(G:\WebDevelopmentFolder\Newsletter Signup\node_modules\express\lib\router\index.js:335:12)
下一步(G:\WebDevelopmentFolder\Newsletter Signup\node\u modules\express\lib\router\index.js:275:10)
位于G:\WebDevelopmentFolder\Newsletter Signup\node\u modules\body parser\lib\read.js:130:5
在invokeCallback(G:\WebDevelopmentFolder\Newsletter Signup\node\u modules\raw body\index.js:224:16)
您需要从对象读取主体
const firstName=req.body.fName;
const lastName=req.body.lName;
const email=req.body.email;
您需要从对象读取主体
const firstName=req.body.fName;
const lastName=req.body.lName;
const email=req.body.email;
错误消息是“无法读取未定义的属性'fName',而不是“'fName'未定义”res.body
是undefined
。错误消息是“无法读取未定义的属性'fName',而不是“'fName'未定义”res.body
是未定义的
。