javascript - 无法更新数据库中的对象

标签 javascript angularjs mongodb express

我正在尝试更新我的一个数据库对象中的数组。

我在发出 put 请求之前检查该对象。但该对象不会在我的 MongoDB 数据库中更新。

client/entry/newEntry.controller.js:

$scope.save = function(form) {
    $scope.submitted = true;
    $scope.entry.date = Date.now;
    $scope.entry.writer = $scope.getCurrentUser;
    $scope.entry.type = 'chapter';
    getHighestArticleId();
    $scope.entry.articleId = articleId;

    if(form.$valid) {
      $http.post('/api/entrys', $scope.entry)
        .success(function(data){
          console.log(' -- posted entry --');
          console.log('data: ', data);
          $scope.entry = data;

          console.log($scope.entry.orphan);
          if($scope.entry.orphan == false){
              $scope.parent.children.push($scope.entry);

              console.log(' -- parent to update --');
              console.log($scope.parent);

              $http.put('/api/entrys/' + $scope.parent._id)
                .success(function(data){
                  console.log(' -- updated parent --');
                  console.log(data);
                });
          }
        });
    }
};


条目api/entry/index.js:

'use strict';

var express = require('express');
var controller = require('./entry.controller');

var router = express.Router();

router.get('/', controller.index);
router.get('/:id', controller.show);
router.get('/:id/children/', controller.getChildren);
router.get('/type/:type', controller.getByType);
router.get('/type/:type/orphan/:hasParent', controller.getByTypeAndOrphan);
router.post('/', controller.create);
router.put('/:id', controller.update);
router.patch('/:id', controller.update);
router.delete('/:id', controller.destroy);

module.exports = router;


api/entry/entry.controller.js:

// Updates an existing entry in the DB.
exports.update = function(req, res) {
  if(req.body._id) { 
      delete req.body._id; 
  }
  Entry.findById(req.params.id, function (err, entry) {
    if (err) { 
        return handleError(res, err); 
    }

    if(!entry) { 
        return res.send(404); 
    }

    var updated = _.merge(entry, req.body);

    updated.save(function (err) {
      if (err) { 
          return handleError(res, err); 
      }
      return res.json(200, entry);
    });
  });
};


<小时/>

编辑


routes.js:

/**
 * Main application routes
 */

'use strict';

var errors = require('./components/errors');

module.exports = function(app) {

  // Insert routes below
  app.use('/api/languages', require('./api/language'));
  app.use('/api/forums', require('./api/forum'));
  app.use('/api/entrys', require('./api/entry'));
  app.use('/api/things', require('./api/thing'));
  app.use('/api/users', require('./api/user'));

  app.use('/auth', require('./auth'));

  // All undefined asset or api routes should return a 404
  app.route('/:url(api|auth|components|app|bower_components|assets)/*')
   .get(errors[404]);

  // All other routes should redirect to the index.html
  app.route('/*')
    .get(function(req, res) {
      res.sendfile(app.get('appPath') + '/index.html');
    });
};

最佳答案

找到答案了。问题出在三个地方。我调用 api 时的第一个

$http.put('/api/entrys/' + $scope.parent._id)
   .success(function(data){
      console.log(' -- updated parent --');
      console.log(data);
});

应该是

$http.put('/api/entrys/' + $scope.parent._id, $scope.parent)
   .success(function(data){
      console.log(' -- updated parent --');
      console.log(data);
});



第二个问题是我的子对象。我传递了整个对象,但只需要 id,所以我的推送应该从此更改

$scope.parent.children.push($scope.entry);

到此

$scope.parent.children.push($scope.entry._id);



最后,我的更新函数本身需要被告知我正在处理子文档。这意味着我必须将其添加到函数中

// Updates an existing entry in the DB.
exports.update = function(req, res) {
  if(req.body._id) { 
      delete req.body._id; 
  }
  Entry.findById(req.params.id, function (err, entry) {
    if (err) { 
        return handleError(res, err); 
    }

    if(!entry) { 
        return res.send(404); 
    }

    var updated = _.merge(entry, req.body);

    entry.markModified('children');

    updated.save(function (err) {
      if (err) { 
          return handleError(res, err); 
      }
      return res.json(200, entry);
    });
  });
};

关于javascript - 无法更新数据库中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29379584/

相关文章:

mongodb - 如何在 ubuntu 16.04.1 上安装 mongodb?

node.js - Mongoose 类型错误 : User is not a constructor

javascript - 单元测试 Karma Jasmine 语法错误 : Parse error on "&" Angular Directive binding

javascript - 无法使用 jQuery 删除附加元素

javascript - 继承后正确保持构造函数

javascript - 如何在不使用指令的情况下更改 html 事件处理程序中的 Angular 属性?

javascript - 多个Highcharts-Vue股票图表中同步x轴缩放

angularjs - 使用 Javascript 多次调用 REST API 或一次调用大型响应正文

javascript - Angularjs:空对象,什么时候只有一键点击?

java - MongoDB Java : how to get write error document?