问题描述
尝试从我正在研究的API项目中创建的mongo数据库中删除字段时出现错误。我不仅对RESTful API还是MongoDB和Express都非常陌生。我遵循了有关YouTube的教程,该教程解释了制作此类API的步骤,因此我做到了,一切都运行良好。现在,我尝试使用自己的自定义字段来重现此API。
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.
基本上,现在我的数据库中填充了两个元素。我已经创建了可以正常使用的获取,添加和更新方法。这是get方法的响应:
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}]
现在有问题的功能是删除功能。我似乎找不到它可能出了什么问题。它与上述YouTube教程中的功能完全相同。我检查了一百次,没有字符错或遗漏。
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
这是server.js的一部分:
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);
}
});
});
这是clients.js部分:
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.
期待阅读您的答案。
推荐答案
我认为'/ api / clients /:_ id'
I think a '/' is missing before '/api/clients/:_id'
它应该是:
app.delete('/api/clients/:_id', function(req, res)
这篇关于无法删除错误的CRUD API(NodeJS,MongoDB,Express)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!