从猫鼬模式获取验证错误

时间:2020-02-01 15:57:24

标签: express mongoose crud

我收到以下错误消息:

 errors:
   { username:
      { ValidatorError: Path `username` is required.

我的模式:

const mongoose = require("mongoose");

const userSchema = new mongoose.Schema({
    email: {
        type: String,
        required: true,
        unique: true,
        trim: true,
    },
    username: {
        type: String,
        required: true,
    },
    password: {
        type: String,
    },
});

const user = mongoose.model("user", userSchema);

module.exports = user;
const createOne = model => async (req, res) => {
    try {
        const doc = await model.create({ ...req.body });
        res.status(201).json({ data: doc });
    } catch (e) {
        //console.error(e);
        res.status(400).end();
    }
};

我通过以下方式使用它:

const crudController = model => ({
    createOne: createOne(model),
});

这是起点:

myRouter
    .route("/blog")
    .post(userController.createOne);

我在顶部导入了usercontroller,我的get路由正在工作。

0 个答案:

没有答案