NodeJs,mongoDB:TypeError:无法读取未定义的属性“全名”
Posted
技术标签:
【中文标题】NodeJs,mongoDB:TypeError:无法读取未定义的属性“全名”【英文标题】:NodeJs , mongoDB : TypeError: Cannot read property 'fullname' of undefined 【发布时间】:2019-04-03 08:09:31 【问题描述】:我想将以 HTML 表单输入的数据存储到 mongoDB 中,但它显示错误消息 - “TypeError: cannot read property 'fullname' of undefined”
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) 在参数 (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) 在 emitTwo (events.js:126:13) 在 Server.emit (events.js:214:7) 在 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>
输出
Here is my Output of Error message
【问题讨论】:
请包含错误和输出作为您问题的内容,而不是图片或外部链接。 应该改成res.body.fullname
等吗?
有效吗?不。我试过它显示同样的错误
【参考方案1】:
您能否尝试使用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())
【讨论】:
很高兴为您提供帮助! :)以上是关于NodeJs,mongoDB:TypeError:无法读取未定义的属性“全名”的主要内容,如果未能解决你的问题,请参考以下文章
nodejs/mongoDB - 类型错误:无法读取未定义的属性“集合”
nodejs/mongoDB - 类型错误:无法读取未定义的属性“集合”
Node js & mongoDB - TypeError: db.collection is not a function
passportjs TypeError:User.authenticate 不是函数