首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ValidationError:名称:路径`name‘是必需的,tech: Path `tech’是必需的。猫鼬

ValidationError:名称:路径`name‘是必需的,tech: Path `tech’是必需的。猫鼬
EN

Stack Overflow用户
提问于 2022-10-14 04:37:19
回答 2查看 37关注 0票数 1

如果我做错了什么,我会得到这个错误name ValidationError: name: Path .,tech: Path 是必需的。在将数据发布到邮递员中时。

这是我的index.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

发布于 2022-10-14 04:45:27

您需要从postman选项中选择json选项。

票数 0
EN

Stack Overflow用户

发布于 2022-10-14 08:44:29

在您的BODY中选择JSON类型

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74064375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档