javascript - 验证后失败:标题:使用 nodejs 的 api 中需要路径“标题”
问题描述
// Creating one
router.post('/', async (req, res) => {
const post = new Post({
title: req.body.title,
category: req.body.category,
content: req.body.content,
author: req.body.author,
postDate: req.body.postdate,
postTags: req.body.posttags
})
try {
const newPost = await post.save()
res.status(201).json(newPost)
} catch (err) {
res.status(400).json({ message: err.message })
}
})
我正在使用节点中的 API 服务器,但在从客户端请求 POS 调用后,响应是
验证后失败:标题:title
需要路径。
mondoDB 的模型是
const mongoose = require('mongoose')
const postSchema = new mongoose.Schema({
title: {
type: String,
required: true
},
category:{
type: String,
required : true
},
content: {
type: String,
required: true
},
author :{
type : String,
required : true
},
postDate: {
type: Date,
required: true,
default: Date.now
},
postTags : {
type : Array,
default : ["Coding"]
}
})
module.exports = mongoose.model('post', postSchema)
解决方案
鉴于您使用的是快递。这在应用程序初始化后进入主文件。这样您的应用程序就能够解析从前端接收到的 json 中的数据。
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
推荐阅读
- google-bigquery - 无法确定表的架构以使用 python 从本地上传到 bigquery
- javascript - 如何将对象数组中单个属性的值相加?
- python - 使用 Python 在循环内异步更新 websocket 服务器
- json - Angular + Spring boot REST,无法发布具有外键的实体
- python - 为什么 Pandas .loc 的点击次数比 MultiIndex.intersection 多?
- macos - MacOS Metal:无法从命令行应用程序捕获 GPU 帧
- sdl - 使用 SDL2,我如何可靠地获取操纵杆轴的初始位置?
- postgresql - 从 relfrozenxid 1749 之前找到 xmin 2965836007
- javascript - URLSearchParams 返回空对象
- python - 试图创建函数来提取特殊字符之间的切片