Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Simple delete request with Postman not working

Options
  • 16-10-2021 12:56am
    #1
    Registered Users Posts: 4,573 ✭✭✭


    Hi guys,

    Have a simple phonebook app created with node.js, express, mongo db and mongoose.

    Get and Post requests and working fine, Post requests are stored on the Mongo database.

    Delete requests are not working however, get a 404 not found status on postman.

    I think I'm not referencing the id of the entry I want to delete properly.

    Also tried using findOneAndDelete({_id: req.params.id}) but that didn't work either.

    When I copy one of my entries from my database these are the details.

    {"_id":{"$oid":"6169f8bc950d87ffe2f6f033"},"name":"nick","number":"087-26387784","__v":{"$numberInt":"0"}}


    Any help appreciated

    Code pasted below

    routes/api.js

    const express = require('express');

    const router = express.Router();

    const Entry = require('../models/entry');


    router.get('/api/entries', (req, res, next) => {

        Entry.find({}).then(entries => {

          res.json(entries)

          console.log(entries)

        })

      }) 

    // add now entry to db

    router.post('/api/entries', function(req, res, next){

        Entry.create(req.body).then(function(entry){

            res.send(entry);

        }).catch(next);

    });


    router.delete('api/entries/:id', function(req, res, next){

        Entry.findByIdAndRemove({_id: req.params.id}).then(function(entry){}).res.send(entry);

        });

    module.exports = router;



Comments

  • Registered Users Posts: 1,703 ✭✭✭JoyPad


    Error 404 means your path is wrong.

    Add a forward-slash in front of api here: router.delete('api/entries/:id',...



Advertisement