Express - app.del()删除所有项目

时间:2013-03-09 17:12:36

标签: javascript express mongoose

我正在使用一个按钮进入删除路线,我正在向它传递一个键,用于识别要删除的团队。在使用路线时,它不会删除一个团队,而是删除所有团队。有什么想法吗?

删除按钮:

        button#teamDelete.btn.btn-danger.btn-mini(type="submit", value="Delete Team") Delete
        script(type='text/javascript')
            $('#teamDelete').live('click',function(){ 
                var teamId = #{teamData.key};
                $.post('/team/' + teamId, { _method : 'delete' }, function(response) {
                    console.log(response);
                });
            });

团队路线:

  app.get('/team'/*, lim("Must be logged in to see teams")*/, getAllTeams, function(req, res){
    util.log('Serving request for url [GET] ' + req.route.path);
    // Pass it the list of all Teams
    res.render('team', {'teamsList' : req.teamsList} );
  });

  /**
    * POST /team
    * Save new Team
    */
  app.post('/team', function(req, res) {
    util.log('Serving request for url [POST] ' + req.route.path);

    // Output to console the contents of req.body
    // console.log('body: ', req.body);
    // console.log('body.teamForm: ', req.body.teamForm);
    // console.log('body.teamForm.name: ', req.body.teamForm.name);
    // console.log('body.teamForm.teamKey: ', req.body.teamForm.teamKey);

    // Get data from teamForm
    var teamForm = req.body.teamForm;

    // Save team in teamForm as new Team
    var name = teamForm.name;
    var team = new Team();
    team.name = name;

    // Save new Team to datbase
    team.save(function(err){
      var message = '';
      var retStatus = '';
      // No error - Successful Save
      if(!err){
        util.log('Successfully created new team: ' + name);
        message = 'Successfully created new team: ' + name;
        retStatus = 'success';
      } 
      // Error - Unsuccessful Save, show error
      else {
        util.log('Error while creating team: ' + name + ' error : ' + util.inspect(err));
        if(err.code === 11000){
          message = 'Team already exists';
        }
        retStatus = 'failure';
      }
      // Return whether the Save was successful
      res.json({
        'retStatus' : retStatus,
        'message' : message
      });
    });
  });

  /**
    * GET /team/:key
    * Get Team details by key
    */
  app.get('/team/:key', function(req, res) {
    util.log('Serving request for url [GET] ' + req.route.path);
    Team.findByKey(req.params.key, function(err, teamData){
      if(!err && teamData){
        teamData = teamData;
        res.render('teamDetails', { 'teamData' : teamData } );
      } else {
        util.log('Error in fetching Team by key : ' + req.params.key);
        res.json({
          'retStatus' : 'failure',
          'msg' : 'Error in fetching Team by key ' + req.params.key
        });
      }
    });
  });

  /**
    * DEL /team/:key
    * Delete Team by key
    */
  app.del('/team/:key', function(req, res) {
    util.log('Serving request for url [DEL] ' + req.route.path);

    util.log(req.params.key);

    Team.remove(req.params.key, function(err){
      var message = '';
      var retStatus = '';
      if (!err) {
        util.log('Successfully deleting Team with key: ' + req.params.key);
        message = 'Successfully deleting Team with key: ' + req.params.key;
        retStatus = 'Success';
      } else {
        util.log('Error deleting Team with key: ' + req.params.key + 'Error: ' + util.inspect(err));
        res.json({
          'retStatus' : 'failure',
          'msg' : 'Error in fetching Team with key ' + req.params.key
        });
      }
    });
  });

1 个答案:

答案 0 :(得分:0)

嗯,你还没有指出,但我认为Team是一个猫鼬模型,在这种情况下你没有正确指定你的conditions参数,它必须是一个密钥对象/价值标准。尝试:

Team.remove({key: req.params.key}, function (err) {});

http://mongoosejs.com/docs/api.html#model_Model.remove