AngularJS在PUT请求后重新加载数据
对于任何一个知道角度的人来说,这应该是一个相当简单的问题。我正在尝试更新在我发出更新对象的PUT请求后显示的数据。下面是一些代码: 邮政服务/Post.jsAngularJS在PUT请求后重新加载数据,angularjs,mongodb,express,request,put,Angularjs,Mongodb,Express,Request,Put,对于任何一个知道角度的人来说,这应该是一个相当简单的问题。我正在尝试更新在我发出更新对象的PUT请求后显示的数据。下面是一些代码: 邮政服务/Post.js 'use strict'; angular.module('hackaboxApp') .factory('Post', function($resource) { return $resource('/api/posts/:id', {id : '@id'}, { 'update': { method: 'P
'use strict';
angular.module('hackaboxApp')
.factory('Post', function($resource) {
return $resource('/api/posts/:id', {id : '@id'}, {
'update': { method: 'PUT' }
})
});
尝试更新数据库/controllers/api.js时执行的服务器端控制器函数
exports.editsave = function(req, res, next) {
var posty = req.body;
console.log(posty._id.toString() + " this is posty");
function callback (err, numAffected) {
console.log(err + " " + numAffected);
if(!err) {
res.send(200);
//res.redirect('/forum');
}
}
Post.update(posty, { id: posty._id.toString() }, callback);
};
这是上述代码的控制台输出:
53c54a0d4960ddc11495d7d7 this is posty
null 0
如您所见,它不会影响任何MongoDB文档,但也不会产生错误
更新帖子时,客户端会发生以下情况:
$scope.saveedit = function() {
console.log($scope.post._id + " post id");
// Now call update passing in the ID first then the object you are updating
Post.update({ id:$scope.post._id }, $scope.post, function() {$location.path('/forum')});
};
在重定向$location.path'/forum'之后,没有任何数据显示为正在更新…当我查看数据库时…也没有任何更改…这就像我错过了保存更改的步骤…但我认为更新PUT请求可以为我做到这一点
加载/论坛路由时,我使用ng init=loadposts:
$scope.loadposts = function() {
$http.get('/api/posts').success(function (data) {$scope.posts = data});
};
在这之后不应该加载所有新数据吗?任何帮助都将不胜感激。谢谢 好的,我知道了
问题是您没有正确使用角度资源api
此代码需要更改:
进入:
至于我自己工作模块中的服务器代码:
exports.update = function (req, res) {
var post == req.post;
post = _.extend(post, req.body);
post.save(function (err) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
});
} else {
res.jsonp(post);
}
});
}) 服务器端输出表明更新查询与数据库中的任何文档都不匹配 我猜您在NodeJS服务器端代码中使用Mongoose来连接mongodb 如果是这种情况,那么update语句似乎不正确 它应该是{u id:},而不是{id:} 此外,条件对象和更新的对象也将交换。 声明应该是这样的:
Post.update({ _id: posty._id.toString() }, posty, callback);
如果您没有使用Mongoose,请详细说明您正在使用的库或更好的库,显示服务器端代码中定义Post变量的代码。我无法回答您的问题。您问的是,当没有任何更新时,如何防止重定向,或者为什么数据库中的数据没有更新?@BenDiamant为什么数据库中的数据没有更新……这就是问题所在……否则,由于我的ng init函数,我想它会在重定向后加载更改的数据。@BenDiamant日志输出告诉我a。它不会因为某种原因改变职位,b。mongoose更新中您的帖子id上没有错误字符串?我想这是你的问题problem@BenDiamant-尝试过-没有更改…仍然找不到所需内容OK-我更改了第一个区块,但出现错误:EditePost。$update不是函数-即将更改您提供的第二个区块我更改为:exports.editsave=functionreq,res,next{//console.logreq.body.posttitle.toString+这是req.body;var post=req.body;post=..extendpost,req.body;post.savefunction err{if err{return res.send400,{message:getErrorMessageerr};}else{res jsonppost;};};};但编译器无法通过$updateerror@ewizard尝试在我的回答中使用更新的角度代码。$update函数是从您创建的Post资源服务派生的。如果您不能将$scope.Post用作Post对象,请创建一个新对象并使用此API。我想我在某个地方读到,$update必须与$resource对象一起使用……因此我尝试此操作:$scope.saveedit=function{var editedpost=Post.get{id:$scope.Post.\u id};//更改editedpost后的Post对象。$updatefunction{$location.path'/forum';},functionerrorResponse{$scope.error=errorResponse.data.message;};我被放了http://localhost:9000/api/posts 404不是Found@ewizard不要使用get。只需从现有的原始创建一个新的
exports.update = function (req, res) {
var post == req.post;
post = _.extend(post, req.body);
post.save(function (err) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
});
} else {
res.jsonp(post);
}
});
Post.update({ _id: posty._id.toString() }, posty, callback);