Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation Error #10

Open
Captainzik opened this issue Jun 9, 2021 · 0 comments
Open

Validation Error #10

Captainzik opened this issue Jun 9, 2021 · 0 comments

Comments

@Captainzik
Copy link

Please who can advise or check what went wrong here, or any solutions. I have checked countless times and compared my code with the one of the instructor, i couldn't find any error.
PLEASE HELP !!

Database Connection is ready...

(node:6064) UnhandledPromiseRejectionWarning: ValidationError: Product validation failed: price: Cast
to Number failed for value "60bfc8111aa4d210245c1e41" (type string) at path "price"
at model.Document.invalidate (C:\Users\RADIOLOGY\Projects\MERN_Course\backend\node_modules\mongoose\lib\document.js:2730:32)
at model.$set (C:\Users\RADIOLOGY\Projects\MERN_Course\backend\node_modules\mongoose\lib\document.js:1319:12)
at model.$set (C:\Users\RADIOLOGY\Projects\MERN_Course\backend\node_modules\mongoose\lib\document.js:1023:16)
at model.Document (C:\Users\RADIOLOGY\Projects\MERN_Course\backend\node_modules\mongoose\lib\document.js:148:12)
at model.Model (C:\Users\RADIOLOGY\Projects\MERN_Course\backend\node_modules\mongoose\lib\model.js:105:12)
at new model (C:\Users\RADIOLOGY\Projects\MERN_Course\backend\node_modules\mongoose\lib\model.js:4706:15)
at C:\Users\RADIOLOGY\Projects\MERN_Course\backend\routes\products.js:19:21
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use node --trace-warnings ... to show where the warning was created)
(node:6064) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejectingr by throwing inside of an async function without a catch block, or by rejecting a promise which was nection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.orgot handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)promise rejections that are not handled will terminate the Node.js process with. (rejection id: 1)
(node:6064) [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

THE CODE

`const {Product} = require('../models/product');
const express = require('express');
const { Category } = require('../models/category');
const router = express.Router();

router.get(/, async (req, res) =>{
const productList = await Product.find();

if(!productList) {
    res.status(500).json({success: false})
} 
res.send(productList);

})

router.post(/, async (req, res) =>{
const category = await Category.findById(req.body.category);
if(!category) return res.status(400).send('Invalid Category')

const product = new Product({
    name: req.body.name,
    description: req.body.description,
    richDescription: req.body.richDescription,        
    image: req.body.image,
    brand: req.body.brand,
    price: req.body.category, 
    category: req.body.category,               
    countInStock: req.body.countInStock,
    rating: req.body.rating,
    numReviews: req.body.numReviews,
    isFeatured: req.body.isFeatured        
})

product = await product.save();

if(!product)
return res.status(500).send('The product cannot be created')
 
res.send(product);

})

module.exports =router;`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant