无法从 mongodb 集合角度 MEAN 堆栈中删除 [英] Cannot delete from mongodb collection angular MEAN stack

查看:46
本文介绍了无法从 mongodb 集合角度 MEAN 堆栈中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im trying to delete a record from my mongodb collection "setlist" in a MEAN stack application.

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

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 被打印到控制台和服务器终端,但该记录并未从集合中删除.

When i click the remove from set list button, the controller and server, fire the correct functions, because the record id is printed to the console and to the servers terminal, but the record isnt deleted from the collection.

推荐答案

这里有两个可能的问题.

Two possible issues here.

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

First and most likely your remove query {_id: mongojs.ObjectId(id)} dont match any document in DB.

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

Second you have some error during remove stage.

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

So i suggest following. Check if remove query actually have documents to remove by doing like this find({_id: mongojs.ObjectId(id)}). If documents will be found then try print err and see what you have there.

文档 https://docs.mongodb.com/manual/参考/方法/db.collection.remove/

希望这会有所帮助.

这篇关于无法从 mongodb 集合角度 MEAN 堆栈中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆