无法更新数据库中的对象

迈克尔·托特·科尔斯高

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

我在发出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);
    });
  });
};



编辑


route.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);
    });
  });
};

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

数据库中的Django更新对象

来自分类Dev

数据库中的Django更新对象

来自分类Dev

无法更新数据库中的列

来自分类Dev

无法更新数据库中的行

来自分类Dev

无法更新数据库中的字段

来自分类Dev

无法更新会议室数据库中的对象

来自分类Dev

Codeigniter更新记录-无法在数据库中更新

来自分类Dev

无法更新数据库

来自分类Dev

无法更新Kinvey数据库中的数据

来自分类Dev

反复更新对象列表中数据库中的记录

来自分类Dev

反复更新对象列表中数据库中的记录

来自分类Dev

对象自动在数据库中更新

来自分类Dev

对象自动在数据库中更新

来自分类Dev

在Django上更新数据库中的模型对象

来自分类Dev

如何更新存储在 SQLite 数据库中的对象

来自分类Dev

似乎无法将对象保存到数据库中

来自分类Dev

无法从PHP中的数据库更新变量

来自分类Dev

数据库更新无法在Meteor方法中运行

来自分类Dev

无法使用SWIFT更新数据库FMDB中的字段

来自分类Dev

无法在Stripe Webhook中更新数据库

来自分类Dev

无法在本地数据库中更新或删除:错误

来自分类Dev

无法更新sqlite数据库中的记录

来自分类Dev

无法更新表单字段中的数据库条目

来自分类Dev

无法更新数据库中的图像路径

来自分类Dev

无法使用SWIFT更新数据库FMDB中的字段

来自分类Dev

我无法更新数据库中的字母密码

来自分类Dev

无法使用php更新mysql数据库中的值

来自分类Dev

如何解释数据库“可更新”属性/错误(3027)。无法更新。数据库或对象是只读的

来自分类Dev

无法更新数据库形式的数据