ERRORS:
(node:10164) UnhandledPromiseRejectionWarning: ValidationError: product validation failed: oem: Path
oemis required., category: Pathcategoryis 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;