Необработанное предупреждение об отказе от обещания - MERN - PullRequest
0 голосов
/ 07 марта 2020

В настоящее время я работаю над приложением 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));
});

Я искал в нескольких разных местах для кто-то, имеющий подобную проблему, но не имел успеха в реализации решений других.

Любая помощь будет принята с благодарностью!

...