我想将以HTML格式输入的数据存储到 mongoDB ,但它显示错误消息-“ TypeError:无法读取未定义的属性'fullname'”
TypeError:无法在读取未定义的属性“全名” 回调时C:\ Users \ admin \ Desktop \ workspace \ lp \ server.js:52:24 (C:\ Users \ admin \ Desktop \ workspace \ lp \ node_modules \ express \ lib \ router \ index.js:164:37) 在param (C:\ Users \ admin \ Desktop \ workspace \ lp \ node_modules \ express \ lib \ router \ index.js:138:11) 在通过 (C:\ Users \ admin \ Desktop \ workspace \ lp \ node_modules \ express \ lib \ router \ index.js:145:5) 在Router._dispatch (C:\ Users \ admin \ Desktop \ workspace \ lp \ node_modules \ express \ lib \ router \ index.js:173:5) 在Object.router [作为句柄] (C:\ Users \ admin \ Desktop \ workspace \ lp \ node_modules \ express \ lib \ router \ index.js:33:10) 在下一个 (C:\ Users \ admin \ Desktop \ workspace \ lp \ node_modules \ connect \ lib \ proto.js:193:15) 在Object.staticMiddleware [作为句柄] (C:\ Users \ admin \ Desktop \ workspace \ lp \ node_modules \ connect \ lib \ Middleware \ static.js:55:61) 在下一个 (C:\ Users \ admin \ Desktop \ workspace \ lp \ node_modules \ connect \ lib \ proto.js:193:15) 在Object.expressInit [作为句柄] (C:\ Users \ admin \ Desktop \ workspace \ lp \ node_modules \ express \ lib \ middleware.js:30:5) 在下一个 (C:\ Users \ admin \ Desktop \ workspace \ lp \ node_modules \ connect \ lib \ proto.js:193:15) 在Object.query [作为句柄] (C:\ Users \ admin \ Desktop \ workspace \ lp \ node_modules \ connect \ lib \ Middleware \ query.js:45:5) 在下一个 (C:\ Users \ admin \ Desktop \ workspace \ lp \ node_modules \ connect \ lib \ proto.js:193:15) 在Function.app.handle (C:\ Users \ admin \ Desktop \ workspace \ lp \ node_modules \ connect \ lib \ proto.js:201:3) 在Server.app (C:\ Users \ admin \ Desktop \ workspace \ lp \ node_modules \ connect \ lib \ connect.js:65:37) 在Server.emit(events.js:214:7)的emitTwo(events.js:126:13)处 parserOnIncoming(_http_server.js:634:12)在 HTTPParser.parserOnHeadersComplete(_http_common.js:117:17)
看起来一切都正确,但我不知道我哪里错了。请帮助我清除此错误。
server.js
var express = require("express");
var app = express();
var path = require("path");
"use strict";
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/lp", {useNewUrlParser: true});
mongoose.set("useCreateIndex", true);
mongoose.Promise = global.Promise;
var Schema = mongoose.Schema;
var engines = require("consolidate");
app.use(express.static(path.join(__dirname, "public")));
app.set("views", __dirname + "/views");
app.engine("html", engines.mustache);
app.set("view engine", "html");
var enrollDataSchema = new Schema({
fullname: {type: String},
emailId: {
type: String,
required: true,
unique: true
},
phone: {
type: Number,
required: true
}
});
var EnrollData = mongoose.model("EnrollData", enrollDataSchema);
app.get("/", function (req, res) {
res.render("index.html");
});
app.post("/enrollform", function (req, res) {
var userinfo = {
fullname: req.body.fullname,
email: req.body.emailId,
contact: req.body.phone
};
var data = new EnrollData(userinfo);
data.save(function (err, result) {
if (err) {
console.log(err);
} else {
res.render("thankyou.html");
}
});
});
app.listen(8000);
console.log("Running at Port 8000");
index.html
<form id="enroll" name="enroll" method="POST" action="/enrollform">
<input type="text" name="fullname" id="fullname" class="form-control" placeholder="Full Name" required=""/>
<input type="email" name="emailId" id="emailId" class="form-control" placeholder="Email ID " required=""/>
<input id="phone" name="phone" placeholder="Phone no" type="tel">
</form>
输出
答案 0 :(得分:0)
能否请您尝试使用body-parser模块。
var bodyParser = require('body-parser')
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())