You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Postman+Mongoose向MongoDB存数据时遇ValidationError求助

Mongoose 存入MongoDB触发ValidationError问题求助

我通过Postman以Body>raw>JSON格式提交数据,试图用Mongoose存入MongoDB,确认数据格式无误,但仍收到如下验证错误:

ValidationError: planModel validation failed: price: Path price is required,duration: Path duration is required., name: Path name is required

已经尝试过平台上的相关解决方案,耗时三天仍未解决,特此求助。

PlanSchema定义

const Planschema = mongoose.Schema({
  name: {
    type: String,
    required: true,
    maxlength: 20,
  },

  duration: {
    type: Number,
    required: true,
  },

  price: {
    type: Number,
    required: true,
  },

  ratingAverage: {
    type: Number,
  },

  discount: {
    type: Number,
    validate: function () {
      return this.discount < 100
    },
  },
})

数据保存代码

module.exports.createPlan = async function createPlan(req, res) {
  let data = req.body

  // 最初尝试的方式
  // let createData = await planModel.create(data);

  // 上面的方式失败后,改用以下方式
  let doc = new planModel(data)
  let userdetails = await doc.save()
  res.json({
    message: 'Data has been Created',
    data: userdetails,
  })
}

内容的提问来源于stack exchange,提问作者Unknown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 04:01:15