简体   繁体   English

使用MEAN堆栈从MongoDB删除数据

[英]Deleting data from MongoDB using MEAN stack

MEAN stack newbie here. MEAN堆栈新手在这里。 I'm having difficulty understanding how delete works in MEAN. 我很难理解删除在MEAN中的工作原理。 I'm using this SO Q&A and tutorial as guides, but whenever I test it out I get an error saying the data can't be deleted. 我正在使用此SO Q&A教程作为指南,但是每当进行测试时,我都会收到一条错误消息,指出无法删除数据。 Can somebody tell me what I've been doing wrong? 有人可以告诉我我做错了什么吗?

Here are my codes: 这是我的代码:

Controller JS 控制器JS

$scope.deleteProduct = function (value, idx) {

    var this_id = value._id;

    // delete
    $http.delete('/api/products/delete:' + this_id)
        .success(function (data) {
            console.log(data);
        })
        .error(function (data) {
            console.log('Error: ' + data);
        })
}

Node Server 节点服务器

app.delete('/api/products/delete:', productController.delete);

Server's "Controller" 服务器的“控制器”

module.exports.delete = function (req, res) {
    Service.remove({
        _id: req.params._id
    }, function (err, service) {
        if (err) {
            res.send(err);
        }
        else {
            res.json({message: "Delete successful."});
        }
    });
}

This is how I understood this. 这就是我对此的理解。 Is this correct? 这个对吗?

  1. Controller JS gets the id to be deleted and calls $http's delete request(?), using said ID and the /api/products/delete: . 控制器JS使用该ID和/api/products/delete:来获取要删除的ID,并调用$ http的delete request(?)。

  2. Node Server sees that I called '/api/products/delete:' and passes the request to Server's Controller to complete the request. 节点服务器看到我调用了“ / api / products / delete:”,并将请求传递到服务器的控制器以完成请求。

  3. Server's Controller deletes the data and returns status. 服务器的控制器删除数据并返回状态。

Where did I go wrong? 我哪里做错了? Please help. 请帮忙。

Also, I've been seeing some posts that say $resource works better than $http . 另外,我一直看到一些帖子说$resource$http更好。 Why? 为什么?

Thank you. 谢谢。

I think you've got a couple things wrong here. 我认为您在这里有几处错误。

In Express in order to use params you need to have something in the route that can be replaced. 在Express中,为了使用params您需要在路由中放置一些可以替换的东西。 ie /api/:id express replaces the :id with whatever you pass in so if you send /api/1 , request.params.id is 1 /api/:id express用传递的内容替换:id ,因此,如果发送/api/1 ,则request.params.id为1

So first problem is your route is 所以第一个问题是你的路线是

app.delete('/api/products/delete:', productController.delete);

tha dosen't mean anything to Express. 这对Express毫无意义。 I think you want 我想你要

app.delete('/api/products/:id', productController.delete);

now req.params.id should contain the parameter you send. 现在req.params.id应该包含您发送的参数。 Note im dropping the underscore here. 请注意,我在此处删除了下划线。 you could use 你可以用

app.delete('/api/products/:_id', productController.delete); and keep the underscore if you like. 并根据需要保留下划线。

Second mistake I think is your Angular code. 我认为第二个错误是您的Angular代码。 you have the : in your call it should just be 你有:在你的电话应该只是

$http.delete('/api/products/' + this_id)

Now you're sending the route with whatever Id you are trying to delete ie 现在,您将使用要删除的ID发送路线,即

/api/products/1

Now Express gets that and can map it to /api/products/:id and replace the id and now your controller should work. 现在,Express可以将其映射到/api/products/:id并替换ID,现在您的控制器应该可以工作了。 barring any other issues. 除非有其他问题。

Edit 编辑

I'm not very familiar with Angular but I think the reason people are saying to use $resource is it is easier. 我对Angular不太熟悉,但是我认为人们说使用$resource的原因是它更容易。 You can directly call the different HTTP verbs directly on the objects themselves objects like product.update and product.delete rather than trying to craft the http calls yourself. 您可以直接在诸如product.updateproduct.delete类的对象本身上直接调用不同的HTTP动词,而不必尝试自己编写HTTP调用。 I'm sure there is a lot more to it than that but its a feature that's built into Angular that can be leveraged. 我敢肯定,除了它之外,还有很多其他功能,但是它内置于Angular中,可以利用。 I think one of the catches is the URLs for the resources just have to be set up a specific way on the server but I believe there was a way to override them in Angular. 我认为要抓住的一个问题是,只需在服务器上设置资源的URL即可,但是我相信在Angular中有一种方法可以覆盖它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM