0

** trying to use the app.delete and trying to delete document from mondo db using delete one...it keep throwing errror. how to solve this error ? ** ``` require('dotenv').config() const express = require('express') const app = express() const PORT = process.env.PORT || 5001 const connectDB = require('./config/db') const errorHandler = require('./middleware/error') const Product =require('./models/product') const cors = require('cors')

 // Connect DB connectDB() // Middleware app.use(cors()) app.use(express.json()) app.use('/auth', require('./routes/authRoutes')) app.use('/admin', require('./routes/adminRoutes')) app.use('/customer', require('./routes/customerRoutes')) app.use('/staff', require('./routes/staffMemberRoutes')) // error handler - should be *last* piece of middleware app.use(errorHandler) app.get('/all-products', (req, res) => { Product.find({}, (error, posts) => { if(error) { res.json({error: 'Unable to fetch products!'}) } else { res.json(posts) } }) }) app.post ('/add-products',(req,res) =>{ console.log("add-products has been fired") const imageurl = req.body.imageurl const title = req.body.title const description = req.body.description const rate = req.body.rate const category = req.body.category const subcategory = req.body.subcategory let product = new Product({ imageurl: imageurl, title: title, description: description, rate: rate, category: category, subcategory: subcategory, }) product.save((error) => { if(error) { res.json({error: 'Unable to save the product!'}) } else { res.json({success: true, message: 'New product Saved'}) } }) }) app.delete('/product/:productId', (req, res) => { const productId = req.params.productId Product.deleteOne({ _id: productId }, (error, result) => { if(error) { res.json({error: 'Unable to delete product'}) } else { res.json({success: true, message: 'Product deleted successfully!'}) } }) }) app.put('/update-product/:productId', (req, res) => { const productId = req.params.productId const imageurl = req.body.imageurl const title = req.body.title const description = req.body.description const rate = req.body.rate const category = req.body.category const subcategory = req.body.subcategory const updatedProduct = { imageurl: imageurl, title: title, description: description, rate: rate, category: category, subcategory: subcategory, } Product.findByIdAndUpdate(productId, updatedProduct, (error, result) => { if(error) { res.json({error: 'Unable to updated the Product'}) } else { res.json({success: true, message: 'Product updated successfully!'}) } }) }) const server = app.listen(PORT, () => { console.log(`Server running on port ${PORT}`) }) // makes giant server errors concise and simple to read process.on('unhandledRejection', (err, promise) => { console.log(`Logged Error: ${err}`) server.close(() => process.exit(1)) })``` 
1
  • What is the error that you are getting? Commented May 16, 2021 at 8:46

1 Answer 1

0
if(error) { res.json({error: 'Unable to delete product'}) } 

In this line, replace res.json with:

res.json({error}) 

And then comment here the output (error message on the console)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.