Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot delete error CRUD API (NodeJS, MongoDB, Express)

I'm getting an error while trying to delete a field from a mongo database I created in an API project I'm working on. I'm still very much new to not only RESTful APIs but also MongoDB and Express. I followed a tutorial on Youtube that explained the steps to go through to make such an API and so I did and everything worked perfectly. Now I'm trying to reproduce this API using my own custom fields.

Basically my database is populated with two elements right now. I've already created get, add and update methods that work properly. Here is the response for the get method :

[{"_id":"58a112564cb325769b9d90de","name":"John Doe","caption":"I like pizza","friends":["id1","id2","id3"],"schedule":[[13,14],[14,15.5]]},{"_id":"58a1178da52bfc07fd25ce3f","name":"Carla Doe","caption":"I hate pizza","__v":0,"friends":null,"schedule":null}]

Now the function that has an issue is the delete function. I can't seem to find what might be wrong with it. It is the exact same function as in the aformentioned Youtube tutorial. I've checked a hundred times over, there is no character wrong or missing.

Here is the error I get in postman :

Cannot DELETE /api/clients/58a1178da52bfc07fd25ce3f

Here is the server.js part :

// Delete client
app.delete('api/clients/:_id', function(req, res){
    var id = req.params._id;
    Client.deleteClient(id, function(err, client){
        if(err){
            throw err;
        }
        else {
            res.json(client);
        }
    });
 });

Here is the clients.js part :

// Delete Clients
module.exports.deleteClient = function (id, client, callback) {
     var query = {_id: id};
    Client.remove(query, client, callback);
};

I don't know if I'm giving you all the information needed to resolve the issue. I can't for the love of me find out where it's coming from.

Looking forward to read your answers.

like image 550
YT98 Avatar asked Nov 30 '22 15:11

YT98


1 Answers

I think a '/' is missing before '/api/clients/:_id'

it should be :

app.delete('/api/clients/:_id', function(req, res)
like image 104
tazee Avatar answered Dec 04 '22 05:12

tazee