【问题标题】:Cannot delete from mongodb collection angular MEAN stack无法从 mongodb 集合角度 MEAN 堆栈中删除
【发布时间】:2017-01-03 16:50:48
【问题描述】:

我试图在 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 会打印到控制台和服务器终端,但记录并没有从集合中删除。

【问题讨论】:

  • 在你的remove回调函数中添加if (err) { res.status(500).json(err)},并使用浏览器的开发工具检查是否返回错误。
  • 不,它不会向控制台返回任何错误。
  • 您确定 {_id: mongojs.ObjectId(id)} 在数据库中有有效记录...尝试使用 find 看看它是否真的会找到结果。我认为转换会更改 id,因此它变得无效。

标签: javascript angularjs node.js mongodb express


【解决方案1】:

这里有两个可能的问题。

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

其次,您在删除阶段遇到了一些错误。

所以我建议跟随。通过像 find({_id: mongojs.ObjectId(id)}) 这样的操作检查删除查询是否确实有要删除的文档。如果可以找到文件,请尝试打印 err 并查看那里有什么。

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

希望这会有所帮助。

【讨论】:

  • 您的第一个建议是正确的。我正在根据记录的 id 从另一个集合向这个集合添加项目,这正确添加了除 id 之外的所有字段,它作为 57c1973ddf6f00b874f6611 而不是 ObjectId("57c1973ddf6f00b874f6611") 插入,所以当我尝试使用id,它找不到它,因为字段的格式不正确。谢谢你。问题解决了:)
猜你喜欢
  • 1970-01-01
  • 2023-04-04
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-26
相关资源
最近更新 更多