是我做错了什么,我得到这个错误**错误验证错误:name:需要路径name
。,tech:路径tech
是必需的。**在 Postman 中POST数据时。
这是我的index.js
const express = require('express')
const mongoose = require('mongoose')
const mongoDB = 'mongodb://127.0.0.1:27017/local_library'
const routers = require('../public/app')
const bodyParser = require("body-parser")
const app = express()
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/app', routers)
app.use(bodyParser.json());
mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true })
const con = mongoose.connection
con.on('open', () => {
console.log('connected..')
})
app.use(express.json)
app.listen(3000, () => {
console.log('server started')
})
schema.js
const mongoose = require('mongoose')
const Schema = mongoose.Schema;
const SomeModelSchema = new Schema({
name: {
type: String,
required: true
},
tech: {
type: String,
required: true
},
})
module.exports = mongoose.model('SomeModel', SomeModelSchema )
app.js
const bodyParser = require('body-parser')
const express = require('express')
const router = express.Router()
const SomeModel = require('../models/schema.js')
router.get('/', async(req, res) => {
try{
const receive = await SomeModel.find()
res.json(receive)
}catch(err){
res.send('Error ' + err)
}
})
router.post ('/', async(req, res) => {
const send = new SomeModel({
name: req.body.name,
tech: req.body.tech,
})
try{
const a1 = await send.save()
res.json(a1)
}catch(err){
res.send('Error' + err)
}
})
module.exports = router
2条答案
按热度按时间piztneat1#
您需要从postman body选项中选择json选项。
ikfrs5lh2#
在正文中选择JSON类型