【问题标题】:Using $resource to delete from database使用 $resource 从数据库中删除
【发布时间】:2015-10-10 19:25:36
【问题描述】:

我无法理解$resource。当用户按下删除按钮时,我正在尝试应用删除方法来删除 MongoDB 数据库条目。我正在使用带有 Express 和 Mongoose 的 Angular 1.4。

这是我的 HTML:

<body ng-app="polls" ng-controller="PollsCtrl">
    Polls:

    <ul>
        <li ng-repeat="question in questions">
            {{ question.questionName }}
            <button ng-click="deletePoll(question._id, $index)">Delete</button>
        </li>

    </ul>

...

这是客户端控制器:

"use strict";

var app = angular.module('polls', ['ngResource']);
app.controller('PollsCtrl', function ($scope, $resource) {

    var Poll = $resource('/api/polls');
    var PollID = $resource('/api/polls/:pollID');

    Poll.query(function (results) {
        $scope.questions = results;
    });

    $scope.questions = [];
    $scope.questionName = '';

    $scope.addPoll = function () {
        var poll = new Poll();
        poll.questionName = $scope.questionName;
        poll.$save(function (result) {
            $scope.questions.push(result);
            $scope.questionName = '';
        });
    };

    $scope.deletePoll = function (id, index) {
        var poll = new PollID();
        poll.$remove(function () {
            $scope.questions.splice(index, 1);
        });
    };
});

这是包含remove 方法的服务器文件:

var express = require('express');
var path = require('path');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');

var app = express();

mongoose.connect('mongodb://localhost/poll-app');

app.use('/controllers', express.static(__dirname + '/controllers'));
app.use('/bower_components', express.static(__dirname + '/bower_components'));

app.get('/', function (req, res) {
    res.sendFile(path.join(__dirname + '/index.html'));
});

app.get('/polls', function (req, res) {
    res.sendFile(path.join(__dirname + '/polls.html'));
});

app.get('/add-poll', function (req, res) {
    res.sendFile(path.join(__dirname + '/add-poll.html'));
});

var pollSchema = new mongoose.Schema({questionName: String});
var Poll = mongoose.model('Poll', pollSchema);

var creater = function (req, res) {
    var poll = new Poll(req.body);
    poll.save(function (err, result) {
        if (err) throw err;
        res.json(result);
    })
};

var list = function (req, res) {
    Poll.find({}, function (err, result) {
        if (err) throw err;
        res.json(result);
    });
};

var remove = function (req, res) {
    console.log(req.params);
    var poll = new Poll(req.body);
    poll.remove({_id: req.params}, function (err, result) {
        if (err) throw err;
        res.json(result);
    });
};

app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

app.post('/api/polls', creater);
app.get('/api/polls', list);
app.delete('/api/polls/:pollID', remove);

app.listen(3000, function () {
    console.log('HI');
});

我很确定错误在于服务器的remove() 方法。当我测试这个时,我实际上得到:

DELETE http://localhost:3000/api/polls 404 (Not Found)

好像它没有路由到我想要的地方。

【问题讨论】:

  • 你必须先加载你的对象,然后再删除它,像这样:var user = User.get({userId:123}, function() { user.abc = true; user.$save ();});
  • @hic1086 我已经用更多代码更新了我的问题以提供上下文

标签: angularjs node.js mongodb express angular-resource


【解决方案1】:
poll.$remove({pollID:id}, function () {
    $scope.questions.splice(index, 1);
})

【讨论】:

  • 您能否在代码 sn-p 中添加一些解释?
  • 您正在传递要在删除中使用的 $resource 的 id
【解决方案2】:

在你的情况下,我认为使用 $http 会更好:

$http.delete('/api/polls/pollID/'+req.params). 

这样,你不必在删除之前获取对象

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 2017-11-29
    • 2013-08-16
    • 1970-01-01
    相关资源
    最近更新 更多