I am just trying to handle the data from the post request. I am handling data in the catch section so as to log it in the console, but still, I am getting the error Unhandled Promise Rejection.
router.post('/', (req, res, next) => {
const product = new Product({
_id: new mongoose.Types.ObjectId(),
name: req.body.name,
price: req.body.price
});
product
.save()
.then(result => {
console.log(result);
});
.catch(err => console.log(err));
res.status(201).json({
message: 'Handling post request to /products.',
createdProduct: product
});
});
Sometimes the db libraries do that. I had similar experience before. You can use global error handler and see what is going on.
process.on('unhandledRejection', function(reason, p){
//call handler here
});
this might shed some more light to the issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.