1
votes

ERRORS:

(node:10164) UnhandledPromiseRejectionWarning: ValidationError: product validation failed: oem: Path oem is required., category: Path category is required. at new ValidationError (C:\Projects\React\ninjas\neo\node_modules\mongoose\lib\error\validation.js:30:11) at model.Document.invalidate (C:\Projects\React\ninjas\neo\node_modules\mongoose\lib\document.js:1957:32) at p.doValidate.skipSchemaValidators (C:\Projects\React\ninjas\neo\node_modules\mongoose\lib\document.js:1825:17) at C:\Projects\React\ninjas\neo\node_modules\mongoose\lib\schematype.js:839:9 at _combinedTickCallback (internal/process/next_tick.js:131:7) at process._tickCallback (internal/process/next_tick.js:180:9) (node:10164) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:10164) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

//Product.js(model)
const mongoose = require('mongoose');

const ProductSchema = new mongoose.Schema({
    oem: {
        type: String,
        required: true
    },
    category: {
        type: String,
        required: true
    },
    date: {
        type: Date,
        default:Date.now
    }
});

module.exports = Product = mongoose.model('product', ProductSchema);

//product.js(routes)
const express = require('express');
const router = express.Router();

var Product = require('../models/Product');


router.get('/', (req, res) => {
  Product.find()
    .sort({ date: -1 })
    .then(prods => res.json(prods));
});

router.post('/newpro', (req, res) => {
    const newProduct = new Product({
      oem: req.body.oem,
      category: req.body.category
    });
    newProduct.save().then(prods => res.json(prods));
});



   

router.delete('/:id', (req, res) => {
  Product.findById(req.params.id)
    .then(prods => prods.remove().then(() => res.json({ success: true })))
    .catch(err => res.status(404).json({ success: false }));
});

module.exports = router;
1

1 Answers

0
votes
router.post('/newpro', (req, res) => {
    const newProduct = new Product({
      oem: req.body.oem,
      category: req.body.category
    });
    newProduct.save().then(prods => res.json(prods));
});

You are getting the values for oem and category from the body of the HTTP POST request.

The error is being raised because one of req.body.oem or req.body.category is null.

I don't know how you are simulating the HTTP REQUEST but double check it,

If your HTTP REQUEST looks good to you I have a 2nd guess, I see you are using no body-parser middleware, keep in mind that req.body is undefined by default.

From the docs:

req.body .
Contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as body-parser and multer.

So perhaps changing your code to the following might fix your problem:

//product.js(routes)
const express = require('express');
const router = express.Router();


var bodyParser = require('body-parser');   
var Product = require('../models/Product'); 

router.use(bodyParser.json()); // for parsing application/json
router.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

// rest of the code

However it should have raised an error when you accessed oem or category in this case, as req.body is undefined by default. If you have any more info let us know.