【问题标题】:POST and Patch is not working in POSTMAN with JSON dataPOST 和 Patch 在 POSTMAN 中无法使用 JSON 数据
【发布时间】:2020-04-30 11:09:41
【问题描述】:

我使用 NodeJS 和 mongodb 作为数据库和 mongoose

[我提供了姓名和电子邮件的值,但它仍然显示需要电子邮件和姓名]

Here is the image

这是我的代码

const mongoose = require('mongoose')

const validator = require('validator')

const User = mongoose.model('User', {

name: {
    type: String,
    required: true,
    trim: true,
    tolowercase: true
},
email: {
    type: String,
    required: true,
    trim: true,
    lowercase: true,
    validate(value){
        if(!validator.isEmail(value)){
            throw new Error('Invalid Email')
        }
    }
},

password: {
    type: String,
    minlength: 7,
    //required: true,
    trim: true,
    validate(value){
        if(value.toLowerCase().includes('password')){
            throw new Error('Password can\'t contains word password')
        }
    }
},

age: {
    type: Number,
    default: 0
}
})

如果我在姓名和电子邮件中评论所有必需的声明,那么它也只需要年龄并将其保存到数据库中

【问题讨论】:

  • 您如何存储/保存来自请求的传入数据。请更新问题中的那部分代码。

标签: node.js json post postman patch


【解决方案1】:

错误响应中的“路径”部分可能表示参数属于 URL 路径而不是正文。

【讨论】:

    【解决方案2】:

    这很可能是因为 Content-Type 不匹配,您从邮递员发送的内容以及您在节点应用程序中收到的内容不匹配。 请检查您是否正在使用 body-parser 并以 JSON 格式解析数据 喜欢: app.use(bodyParser.json()); 不喜欢: app.use(bodyParser.urlencoded({extended: false })); 我认为您必须从邮递员发送带有 Content-Type: application/json 的标头,并将其作为 urlencoded 接收,反之亦然。

    【讨论】:

      猜你喜欢
      • 2017-06-16
      • 2021-12-31
      • 1970-01-01
      • 2016-07-08
      • 2020-07-17
      • 2022-07-12
      • 2016-12-24
      • 1970-01-01
      • 2020-04-29
      相关资源
      最近更新 更多