我在设置RESTful API实践作为教程时遇到问题,我的设置如下工作但我的问题是当我打开Mongo和db.users.find()相应的数据时,我只得到了“ firstName“来自我的HTML表单。
// ROUTE SETTINGS
var express = require("express");
var app = express();
var port = 3000;
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var mongoose = require("mongoose");
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost:27017/node-demo");
var nameSchema = new mongoose.Schema({
firstName: String,
lastNameName: String
});
var User = mongoose.model("User", nameSchema);
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
app.post("/addname", (req, res) => {
var myData = new User(req.body);
myData.save()
.then(item => {
res.send("item saved to database");
})
.catch(err => {
res.status(400).send("unable to save to database");
});
});
app.listen(port, () => {
console.log("Server listening on port " + port);
});
下面的HTML表单:
<!DOCTYPE html>
<html>
<head>
<title>Intro to Node and MongoDB</title>
</head>
<body>
<h1>Into to Node and MongoDB</h1>
<form method="POST" action="/addname">
<label>Enter Your Name</label><br>
<input type="text" name="firstName" placeholder="Enter first name..." required>
<input type="text" name="lastName" placeholder="Enter last name..." required>
<input type="submit" value="Add Name">
</form>
</body>
</html>
有什么东西我遗漏了吗?我已经尝试了db.find的所有变体,但我得到的就是
{"_id" : ObjectId<"5b208054b0ef0f0d8c238b1f">, "firstName": "sgez", "_v":0}
我如何能够从MongoDB中的HTML表单访问lastName部分