未处理的承诺拒绝警告-MERN

时间:2020-03-06 22:45:12

标签: mongodb express mongoose mern

我目前正在开发一个MERN应用,该应用可在输入字段中从用户那里获取信息并显示回来。触发onSubmit后出现此错误:

> (node:18820) UnhandledPromiseRejectionWarning: ValidationError: 
item validation failed: brand: Path `brand` is required., trailer_type:
Path `trailer_type` is required.

型号:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

// Create Schema 
const ItemSchema = new Schema({
    brand: {
        type: String,
        required: true
    },
    trailer_type: {
        type: String,
        required: true
    },
    date: {
        type: Date,
        default: Date.now
    }
});

module.exports = Item = mongoose.model('item', ItemSchema);

路由器

const express = require('express');
const router = express.Router();
const auth = require('../../middleware/auth');

var bodyParser = require('body-parser');

// Item Model
const Item = require('../../models/Item');

router.use(bodyParser.json());
router.use(bodyParser.urlencoded({ extended: true }));

router.post('/', auth, (req, res) => {
    const newItem = new Item({
        brand: req.body.brand,
        trailer_type: req.body.trailer_type,
        date: req.body.date
    });

    newItem.save()
    .then(item => res.json(item));
});

我已经在几个不同的地方搜索了一个存在类似问题的人,但是在实现其他人的解决方案方面没有成功。

任何帮助将不胜感激!

0 个答案:

没有答案
相关问题