javascript - Can't update object in database -
i'm trying update array in 1 of database objects.
i'm checking object before making put
request. object won't update in mongodb database.
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 update --'); console.log($scope.parent); $http.put('/api/entrys/' + $scope.parent._id) .success(function(data){ console.log(' -- updated parent --'); console.log(data); }); } }); } };
entry 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 existing entry in 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); }); }); };
edit
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')); // undefined asset or api routes should return 404 app.route('/:url(api|auth|components|app|bower_components|assets)/*') .get(errors[404]); // other routes should redirect index.html app.route('/*') .get(function(req, res) { res.sendfile(app.get('apppath') + '/index.html'); }); };
found answer. problem in 3 places. first in call api
$http.put('/api/entrys/' + $scope.parent._id) .success(function(data){ console.log(' -- updated parent --'); console.log(data); });
should instead be
$http.put('/api/entrys/' + $scope.parent._id, $scope.parent) .success(function(data){ console.log(' -- updated parent --'); console.log(data); });
the second problem child object. passed entire object, needed id, push should change this
$scope.parent.children.push($scope.entry);
to this
$scope.parent.children.push($scope.entry._id);
finally update function needed informed handling sub-document. meant had add function
// updates existing entry in 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); }); }); };
Comments
Post a Comment