is anything I'm doing wrong, I get this error Error ValidationError: name: Path name
is required., tech: Path tech
is required. while POST the data in postman.
here is my 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
CodePudding user response:
You need to select json option from postman body option.
CodePudding user response:
choose type JSON in the your BODY