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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Simple delete request with Postman not working

  • 15-10-2021 11:56pm
    #1
    Registered Users, Registered Users 2 Posts: 4,620 ✭✭✭


    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 Posts: 1,704 ✭✭✭JoyPad


    Error 404 means your path is wrong.

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



Advertisement