I want to store the data entered in HTML form into mongoDB but it shows error message - "TypeError: cannot read property 'fullname' of undefined"
TypeError: Cannot read property 'fullname' of undefined at C:\Users\admin\Desktop\workspace\lp\server.js:52:24 at callbacks (C:\Users\admin\Desktop\workspace\lp\node_modules\express\lib\router\index.js:164:37) at param (C:\Users\admin\Desktop\workspace\lp\node_modules\express\lib\router\index.js:138:11) at pass (C:\Users\admin\Desktop\workspace\lp\node_modules\express\lib\router\index.js:145:5) at Router._dispatch (C:\Users\admin\Desktop\workspace\lp\node_modules\express\lib\router\index.js:173:5) at Object.router [as handle] (C:\Users\admin\Desktop\workspace\lp\node_modules\express\lib\router\index.js:33:10) at next (C:\Users\admin\Desktop\workspace\lp\node_modules\connect\lib\proto.js:193:15) at Object.staticMiddleware [as handle] (C:\Users\admin\Desktop\workspace\lp\node_modules\connect\lib\middleware\static.js:55:61) at next (C:\Users\admin\Desktop\workspace\lp\node_modules\connect\lib\proto.js:193:15) at Object.expressInit [as handle] (C:\Users\admin\Desktop\workspace\lp\node_modules\express\lib\middleware.js:30:5) at next (C:\Users\admin\Desktop\workspace\lp\node_modules\connect\lib\proto.js:193:15) at Object.query [as handle] (C:\Users\admin\Desktop\workspace\lp\node_modules\connect\lib\middleware\query.js:45:5) at next (C:\Users\admin\Desktop\workspace\lp\node_modules\connect\lib\proto.js:193:15) at Function.app.handle (C:\Users\admin\Desktop\workspace\lp\node_modules\connect\lib\proto.js:201:3) at Server.app (C:\Users\admin\Desktop\workspace\lp\node_modules\connect\lib\connect.js:65:37) at emitTwo (events.js:126:13) at Server.emit (events.js:214:7) at parserOnIncoming (_http_server.js:634:12) at HTTPParser.parserOnHeadersComplete (_http_common.js:117:17)
It looks everything is correct but I don't know where i am wrong. Kindly help me to clear this error.
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>
Output
res.body.fullname
etc instead? – Andy