繁体   English   中英

无法从mongodb集合中删除角度MEAN堆栈

[英]Cannot delete from mongodb collection angular MEAN stack

我正在尝试从MEAN堆栈应用程序中的mongodb集合“ setlist”中删除一条记录。

index.html

<table class="table">
            <thead>
                <tr>
                    <th>Artist</th>
                    <th>Title</th>
                    <th>Key</th>
                    <th>Action</th>
                    <th>&nbsp;</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="setTrack in setlist" ng-click="clicked">
                    <td><h4>{{setTrack.Artist}}</h4></td>
                    <td><h4>{{setTrack.Title}}</h4></td>
                    <td><h4>{{setTrack.Key}}</h4></td>
                    <td><button class="btn btn-success" ng-click="removeFromSL(setTrack._id)">Remove from set list</button></td>
                </tr>
            </tbody>
        </table>

controller.js

    var myApp = angular.module('myApp', []);
myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
    console.log("Hello World from controller");

    var refresh = function() {
        $http.get('/setlist').success(function(response) {
            console.log("I got the data i requested");
            $scope.setlist = response;
            $scope.setTrack = "";
        });
    };

    $scope.addToSL = function(id) {
        $http.get('/tracks/' + id).success(function(response) {
            console.log(response);
            $http.post('/setlist', response).success(function(response) {
                console.log(response);
                refresh();
            });
        });
    };

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

server.js

    var express = require('express');
var app = express();
var mongojs = require('mongojs');
var db = mongojs('tracks', ['tracks']);
var db1 = mongojs('setlist', ['setlist']);
var bodyParser = require('body-parser');

app.use(express.static(__dirname = '\public'));
app.use(bodyParser.json());

app.get('/setlist', function (req, res) {
    console.log("I recieved a get request");

    db1.setlist.find(function (err, docs) {
        console.log(docs);
        res.json(docs);
    });
});

app.delete('/setlist/:id', function (req, res) {
    var id = req.params.id;
    console.log(id);
    db1.setlist.remove({_id: mongojs.ObjectId(id)}, function(err, doc) {
        res.json(doc);
    });
});

app.listen(3000);
console.log("Server running on port 3000");

当我单击“从集合列表中删除”按钮时,控制器和服务器将触发正确的功能,因为记录ID已打印到控制台和服务器终端,但记录并未从集合中删除。

这里有两个可能的问题。

首先,很可能是您的删除查询{_id: mongojs.ObjectId(id)}与数据库中的任何文档{_id: mongojs.ObjectId(id)}匹配。

其次,在删除阶段您有一些错误。

所以我建议以下。 通过执行find({_id: mongojs.ObjectId(id)})来检查删除查询是否确实有要删除的文档。 如果可以找到文档,请尝试打印err并查看其中的内容。

文件https://docs.mongodb.com/manual/reference/method/db.collection.remove/

希望这可以帮助。

暂无
暂无

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

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