Express JS / Mangoose:创建对象时出现错误请求错误

Express JS / Mangoose : Bad Request error creating an object

我想从表单数据创建一个 Mangoose 对象。

这里是HTML页面的JS代码:

const easyMDE = new EasyMDE({element: document.getElementById('body_content')});

    $(document).ready(function () {
        $("form").submit(function (event) {
            var formData = {
                'body_content': easyMDE.value(),
                'name': $('input[name=name]').val(),
                'seo_title': $('input[name=seo_title]').val(),
                'image_url': $('input[name=image_url]').val(),
                'template': $('input[name=template]').val(),
                'type': $('input[name=type]').val(),
                'slug': $('input[name=slug]').val(),
                'seo_description': $('input[name=seo_description]').val()
            };

            $.ajax({
                type: "POST",
                url: "/nxapi/page/",
                data: formData,
                dataType: "json",
                encode: false,
            }).done(function (data) {
                console.log(data);
            });

            event.preventDefault();
        });
    });

这是控制器代码:

const Page = require('../models/Page');

exports.createPage = (req, res, next) => {
  const page = new Page({
    type: req.body.type,
    name: req.body.name,
    template: req.body.template,
    image_url: req.body.image_url,
    seo_title: req.body.seo_title,
    seo_description: req.body.seo_description,
    body_content: req.body.body_content,
  });
  page.save().then(
    () => {
      res.status(201).json({
        message: 'Post saved successfully!'
      });
    }
  ).catch(
    (error) => {
      res.status(400).json({
        error: error
      });
    }
  );
};

还有我的模特:

const mongoose = require('mongoose');

const pageSchema = mongoose.Schema({
  type: { type: String, required: true },
  name: { type: String, required: true },
  template: { type: String, required: true },
  slug: { type: String, required: true },
  image_url: { type: String },
  seo_title: { type: String },
  seo_description: { type: String },
  body_content: { type: String },
  created: { type: Date, default: Date.now() },
  modified: { type: Date, default: Date.now() },
});

module.exports = mongoose.model('Page', pageSchema);

当我提交表单时,我遇到了这个错误:

Page validation failed: slug: Path `slug` is required.

我尝试以另一种方式对 JSON 中的表单数据进行编码,但我仍然检查提交的数据,并且控制器功能很好地接收了所有内容。例如,我可以 console.log(req.body.type),并且正确显示了值。

如果我 console.log req.body 变量,这就是我得到的:

{
  body_content: 'Test',
  name: 'Test',
  seo_title: 'Test SEO',
  image_url: 'image.jpg',
  template: 'article-v1',
  type: 'article',
  slug: 'test',
  seo_description: 'Description SEO'
}

我在 app.js 中添加了:

const bodyParser = require("body-parser")
app.use(bodyParser.urlencoded({ extended: true }));

我不明白我做错了什么。 有人可以帮我吗?

非常感谢,

创建对象时需要指定slug字段。

  const page = new Page({
    type: req.body.type,
    name: req.body.name,
    template: req.body.template,
    image_url: req.body.image_url,
    seo_title: req.body.seo_title,
    seo_description: req.body.seo_description,
    body_content: req.body.body_content,
    slug: req.body.slug
  });