Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

145
Views
DELETE request doesn't work in node js mongodb

I followed this API tutorial and I managed to get everything running except the DELETE method.

This is the delete method :

// Deleting one
router.get('/:id' ,getOrder, async (req, res) => {
    try {
        await res.order.remove();
        res.json({ message: 'Order Deleted' })
    } catch (error) {
        res.status(500).json({ message: error.message })
    }
})

Function to findID :

async function getOrder(req, res, next) {
    let order
    try {
        order = await Order.findById(req.params.id)
        if (order == null) {
            return res.status(404).json({ message: 'cannot find Order' })
        }
    } catch (error) {
        return res.status(500).json({ message: error.message })
    }

    res.order = order
    next()
}

This my server.js file

require('dotenv').config();

const express = require('express');
const app = express();
const mongoose = require('mongoose');
let port = process.env.PORT || 3000;

mongoose.connect(
  process.env.DATABASE_URL, 
  {
    useNewUrlParser: true,
    useUnifiedTopology: true
  }
);

const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error: "));
db.once("open", function () {
  console.log("Connected to db successfully");
});

app.use(express.json())

const ordersRouter = require('./routes/orders')
app.use('/orders', ordersRouter)


app.listen(port , () => console.log('server started'))

And this is the response I get after the DELETE request

response from server

9 months ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.