未找到快速删除路由

时间:2016-02-05 10:18:23

标签: angularjs node.js mongodb express

我正在尝试使用id

删除mongo中的特定记录

在角度我从控制器传递Id 它给出了错误代码,说找不到404 节点服务器端文件中的代码是:

app.delete('/contactlist/id', function (req, res) {
var id = req.params.id;
console.log("in delete"+ id);
db.contactlist.remove({id : mongojs.ObjectId(id)}, function ( err , doc){
    res.json(doc);
  });
});

和角度控制器中的代码:

    $scope.remove = function (id){
    console.log(id);
    $http.delete('/contactlist'+ id ).success( function(response) {
        refresh();
    });
}

2 个答案:

答案 0 :(得分:1)

一切看起来都不错,除了

$http.delete('/contactlist'+ id ) //you forgot additional slash, '/contactlist/'+ id
.success( function(response) {
    refresh();
});

app.delete('/contactlist/id', function (req, res) { //this must be a param '/contactlist/:id'
    var id = req.params.id;
    console.log("in delete"+ id);
    db.contactlist.remove({id : mongojs.ObjectId(id)}, function ( err , doc){
        res.json(doc);
    });
});

总结

$http.delete

添加斜杠
$http.delete('/contactlist/'+ id).success( function(response) {
    refresh();
});

在您的服务器中创建id个参数。

app.delete('/contactlist/:id', function (req, res) { //colons are important
    var id = req.param.id;
    console.log("in delete"+ id);
    db.contactlist.remove({id : mongojs.ObjectId(id)}, function ( err , doc){
        res.json(doc);
    });
});

答案 1 :(得分:0)

尝试以下代码

#include <iostream>

using namespace std;

int main()
{
    typedef int (*board_ptr)[21];

    int board_data[21][21];
    board_ptr board = (board_ptr)&board_data[10][10];

    for (int i = -10; i <= 10; ++i)
        for (int j = -10; j <= 10; ++j)
            board[i][j] = 0;
    board[-10][-10] = 1;
    board[-10][10] = 2;
    board[10][-10] = 3;
    board[10][10] = 4;
    board[0][0] = 5;
    for (int i = -10; i <= 10; ++i)
    {
        for (int j = -10; j <= 10; ++j)
        {
            cout << " " << board[i][j];
        }
        cout << endl;
    }
    return 0;
}

您可以在此处查看文档,使用网址传递参数:http://expressjs.com/en/api.html