У меня есть некоторые проблемы.

проблема в том, что данные не сохраняются в mongodb.

я не знаю почему не работает нормально.

источники:

write.ejs

{amp}lt;form method='post' action="/post/write"{amp}gt; {amp}lt;table border="1"{amp}gt; {amp}lt;tr{amp}gt; {amp}lt;td{amp}gt;id{amp}lt;/td{amp}gt; {amp}lt;td{amp}gt;{amp}lt;input type='text' name='postid' id='postid'{amp}gt;{amp}lt;/td{amp}gt; {amp}lt;/tr{amp}gt; {amp}lt;tr{amp}gt; {amp}lt;td{amp}gt;title{amp}lt;/td{amp}gt; {amp}lt;td{amp}gt;{amp}lt;input type='text' name='title' id='title'{amp}gt;{amp}lt;/td{amp}gt; {amp}lt;/tr{amp}gt; {amp}lt;tr{amp}gt; {amp}lt;td{amp}gt;content{amp}lt;/td{amp}gt; {amp}lt;td{amp}gt;{amp}lt;input type='text' name='content' id='content'{amp}gt;{amp}lt;/td{amp}gt; {amp}lt;/tr{amp}gt; {amp}lt;tr{amp}gt; {amp}lt;td{amp}gt; {amp}lt;input type="submit" value="submit!"{amp}gt; {amp}lt;/td{amp}gt; {amp}lt;/tr{amp}gt; {amp}lt;/table{amp}gt; {amp}lt;/form{amp}gt; 

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) ={amp}gt; { Post.findAll() .then((posts) ={amp}gt; { if (!posts.length) return res.status(404).send({ err: 'post not found from get /'}) res.render('posts', {data:posts}) }) .catch(err ={amp}gt; res.status(500).send(err)) }) app.get('/postid/:postid', (req, res) ={amp}gt; { Post.findOneByPostid(req.params.postid) .then((post) ={amp}gt; { 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 ={amp}gt; res.status(500).send(err)) }) // this is example. it would be comment code. app.post('/', (req, res) ={amp}gt; { Post.create(req.body) .then(post ={amp}gt; res.send(post)) .catch(err ={amp}gt; res.status(500).send(err)) }) app.get('/write', (req, res) ={amp}gt; { res.render('write') }) app.post('/write', (req, res) ={amp}gt; { console.log('req.body : '   req.body) Post.create(req.body) .then(post ={amp}gt; res.send(post)) .catch(err ={amp}gt; res.status(500).send(err)) }) app.put('/postid/:postid', (req, res) ={amp}gt; { Post.updateByPostid(req.params.postid, req.body) .then(post ={amp}gt; res.send(post)) .catch(err ={amp}gt; res.status(500).send(err)); }); app.delete('/postid/:postid', (req, res) ={amp}gt; { Post.deleteByPostid(req.params.postid) .then(() ={amp}gt; res.sendStatus(200)) .catch(err ={amp}gt; res.status(500).send(err)); }); module.exports = app 

проблема в том, что данные не сохраняются в mongodb. когда я отправляю значения по маршруту ниже

HTTP: // локальный: 3000 / запись / запись

и результаты на mongodb

 {"_id":"5e038fd7d17c122ee084b397","published_date":"2019-12-25T16:35:35.566Z","__v":0} 

supervisor app.js

 {amp}gt; Starting child process with 'node app.js' Watching directory {amp}gt; 'C:workspacemy_blog' for changes. Press rs for restarting the {amp}gt; process. (node:14860) DeprecationWarning: current URL string parser is {amp}gt; deprecated, and will be removed in a future version. To use the new {amp}gt; parser, pass option { useNewUrlParser: true } to MongoClient.connect. {amp}gt; node server running successfully 3000 (node:14860) DeprecationWarning: {amp}gt; current Server Discovery and Monitoring engine is deprecated, and will {amp}gt; be removed in a future version. To use the new Server Discover and {amp}gt; Monitoring engine, pass option { useUnifiedTopology: true } to the {amp}gt; MongoClient constructor. Connected to mongod server {amp}gt; req.body : [object {amp}gt; Object] 

req.body — это [объект{amp}gt; объект], как я могу увидеть этот объект?

Спасибо

Youggyu Ли

новый участник этого сайта. Будьте внимательны, спрашивая разъяснения, комментируя и отвечая. Проверьте наш

Правила поведения

,