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

此生再无相见时 提交于 2019-12-13 16:41:45

问题


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.


回答1:


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

it should be :

app.delete('/api/clients/:_id', function(req, res)


来源:https://stackoverflow.com/questions/42207958/cannot-delete-error-crud-api-nodejs-mongodb-express

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!