node.js - 如何修复 MongoDB 文档只是 id 和 __v
问题描述
当我尝试向数据库添加帖子时,JSON Body 非常好,但是当我检索这些时,它只显示 id 和 __v (versionKey)
我尝试过以不同的方式使用 body-parser,例如...
app.use('bodyParser.json()')
var urlencodedParser = bodyParser.urlencoded({ extended: false })
var jsonParser = bodyParser.json();
最后两个包含在 app.post 方法中。
这是我的 index.js
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const app = express();
const port = 4000;
const postRoutes = express.Router();
let Post = require('./post.model');
var urlencodedParser = bodyParser.urlencoded({ extended: false })
var jsonParser = bodyParser.json();
app.use(cors());
app.use('/', express.static('public'));
app.use(bodyParser.json());
app.use('/posts', postRoutes);
mongoose.connect("mongodb://127.0.0.1:27017/mern-blog-test", { useNewUrlParser: true });
const connection = mongoose.connection;
connection.once('open', function() {
console.log('Succesfully established connection to MongoDB Database');
})
postRoutes.route('/').get(function (req, res) {
Post.find(function (err, posts) {
if (err) {
console.log(err);
} else {
res.json(posts);
}
});
});
postRoutes.route(':id').get(function(req,res) {
let id = req.params.id;
Post.findById(id, function(err, post) {
res.json(post);
});
});
// todoRoutes.route('/add').post(function (req, res) {
// let todo = new Todo(req.body);
// todo.save()
// .then(todo => {
// res.status(200).json({ 'todo': 'todo added successfully' });
// })
// .catch(err => {
// res.status(400).send('adding new todo failed');
// });
// });
app.get('/create', function(req,res){
res.sendFile(__dirname + '/public/add.html');
})
app.post('/makepost', urlencodedParser, function(req,res) {
console.log(req.body);
let post = new Post(req.body);
post.save()
.then(todo => {
res.sendFile(__dirname + '/public/add.html')
})
.catch(err => {
res.status(400).send('adding new post failed!');
});
});
这是我的表格
<label for="title">Title</label>
<input type="text" name="title">
<label for="category">Category</label>
<select name="category">
<option>Coding</option>
<option>Music</option>
<option>Books</option>
<option>Other Stuff</option>
</select>
<label for="content">Content</label>
<input type="text" name="content">
<input type="submit">
</form>
还有我的 post.model.js
const Schema = mongoose.Schema;
let Post = new Schema({
post_title: {
type: String
},
post_date: {
type: Date
},
post_shortdesc: {
type: String
},
post_content: {
type: String
}
});
module.exports = mongoose.model('Post', Post);
我希望输出是......
[{"_id":"5d76a7e78b55cd3309b91ecf", "title": "blah", category: "blah", "content": "blah", "__v":0}]
但它是...
[{"_id":"5d76a7e78b55cd3309b91ecf","__v":0}]
解决方案
对不起,我自己想通了。表单正在输出模型中没有的字段。将表格更改为...
<form id="add-form" method="POST" action="/makepost">
<label for="post_title">Title</label>
<input type="text" name="post_title">
<!-- <label for="post_category">Category</label>
<select name="post_category">
<option>Coding</option>
<option>Music</option>
<option>Books</option>
<option>Other Stuff</option>
</select> -->
<label for="post_date">Content</label>
<input type="date" name="post_date">
<label for="post_content">Content</label>
<input type="text" name="post_content">
<input type="submit">
</form>
基本上只是更改了输入字段的名称以匹配模型。
推荐阅读
- shell - 为什么 sed 使用 shell 变量失败,但使用它们的值成功?
- excel - VBA 错误问题
- c# - HelixToolkit 选择几何
- javascript - 我想使用 intercooler.js 将内容加载到 bootstrap 4 模态中
- javascript - 如何获取对前端代码的 mongodb 查询
- python - `readlines()` 会关闭在 python 中打开的文件吗?
- r - 大型数据集最佳实践的内部联接
- javascript - 如何使用javascript在手机上的网页上录制音频和播放音乐?
- javascript - 如何在没有利用者的情况下确定多次点击?
- go - k8s api 服务器前面的 Golang 代理挂在 kubectl delete pod 上并带有“WINDOW_UPDATE”