我有问题。
问题是数据没有正常存储到mongodb。
我不知道为什么不能正常工作。
来源:
write.ejs
<form method='post' action="/post/write">
<table border="1">
<tr>
<td>id</td>
<td><input type='text' name='postid' id='postid'></td>
</tr>
<tr>
<td>title</td>
<td><input type='text' name='title' id='title'></td>
</tr>
<tr>
<td>content</td>
<td><input type='text' name='content' id='content'></td>
</tr>
<tr>
<td>
<input type="submit" value="submit!">
</td>
</tr>
</table>
</form>
post.js
const mongoose = require('mongoose')
const postSchema = new mongoose.Schema({
postid: String,
title: String,
content: String,
published_date: { type: Date, default: Date.now}
})
postSchema.static.create = function (payload){
// console.log('payload' + payload)
// console.log('post' + post)
const post = new this(payload)
return post.save()
}
postSchema.statics.findAll = function (){
return this.find({});
}
postSchema.statics.findOneByPostid = function (postid) {
return this.findOne({ postid });
};
postSchema.statics.updateByPostid = function (postid, payload) {
// { new: true }: return the modified document rather than the original. defaults to false
return this.findOneAndUpdate({ postid }, payload, { new: true });
};
postSchema.statics.deleteByPostid = function (postid) {
return this.remove({ postid });
};
module.exports = mongoose.model('Post', postSchema)
post.js
const express = require('express')
const Post = require('../models/post')
const bodyParser = require('body-parser')
const app = express.Router()
app.use(bodyParser.json())
app.get('/', (req, res) => {
Post.findAll()
.then((posts) => {
if (!posts.length) return res.status(404).send({ err: 'post not found from get /'})
res.render('posts', {data:posts})
})
.catch(err => res.status(500).send(err))
})
app.get('/postid/:postid', (req, res) => {
Post.findOneByPostid(req.params.postid)
.then((post) => {
if (!post) return res.status(404).send({ err: 'post not found from get /postid:/postid '})
// res.send(`findone successfully: ${post}`)
res.render('post', {data:post})
})
.catch(err => res.status(500).send(err))
})
// this is example. it would be comment code.
app.post('/', (req, res) => {
Post.create(req.body)
.then(post => res.send(post))
.catch(err => res.status(500).send(err))
})
app.get('/write', (req, res) => {
res.render('write')
})
app.post('/write', (req, res) => {
console.log('req.body : ' + req.body)
Post.create(req.body)
.then(post => res.send(post))
.catch(err => res.status(500).send(err))
})
app.put('/postid/:postid', (req, res) => {
Post.updateByPostid(req.params.postid, req.body)
.then(post => res.send(post))
.catch(err => res.status(500).send(err));
});
app.delete('/postid/:postid', (req, res) => {
Post.deleteByPostid(req.params.postid)
.then(() => res.sendStatus(200))
.catch(err => res.status(500).send(err));
});
module.exports = app
问题是数据没有正常存储到mongodb。 当我在以下路线发送值时
http://localhost:3000/post/write
和结果在mongodb
{"_id":"5e038fd7d17c122ee084b397","published_date":"2019-12-25T16:35:35.566Z","__v":0}
supervisor app.js
> Starting child process with 'node app.js' Watching directory
> 'C:\workspace\my_blog' for changes. Press rs for restarting the
> process. (node:14860) DeprecationWarning: current URL string parser is
> deprecated, and will be removed in a future version. To use the new
> parser, pass option { useNewUrlParser: true } to MongoClient.connect.
> node server running successfully 3000 (node:14860) DeprecationWarning:
> current Server Discovery and Monitoring engine is deprecated, and will
> be removed in a future version. To use the new Server Discover and
> Monitoring engine, pass option { useUnifiedTopology: true } to the
> MongoClient constructor. Connected to mongod server
> req.body : [object > Object]
req.body是[object> Object]我怎么看这个对象?
谢谢