Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Simple delete request with Postman not working

  • 16-10-2021 12:56AM
    #1
    Registered Users, Registered Users 2, Paid Member Posts: 4,749 ✭✭✭


    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, Registered Users 2, Paid Member Posts: 1,711 ✭✭✭JoyPad


    Error 404 means your path is wrong.

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

    I subscribed to keep boards.ie alive!
    boards.ie: Now Ye're Subscribing



Advertisement