我正在尝试更新我的一个数据库对象中的数组。
我在发出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] 删除。
我来说两句