Javascript Ionic/NodeJS/MEAN-Put清除对象数据
我目前正在使用Node/Express和Ionic/Angular开发一个MEAN stack应用程序。我有一个页面,用户可以在其中编辑/删除特定对象的内容。delete函数起作用,但当我单击edit并触发put/update函数时,它会清除除id号和“_v”:0之外的数据,而不是更新对象 我已经与Postman一起检查了服务器端API,它可以用body内容类型x-form-urlencoded进行更新。我的直觉是在客户端正确获取数据。非常感谢您的帮助 以下是我的离子/角度控制器代码:Javascript Ionic/NodeJS/MEAN-Put清除对象数据,javascript,angularjs,node.js,ionic,mean-stack,Javascript,Angularjs,Node.js,Ionic,Mean Stack,我目前正在使用Node/Express和Ionic/Angular开发一个MEAN stack应用程序。我有一个页面,用户可以在其中编辑/删除特定对象的内容。delete函数起作用,但当我单击edit并触发put/update函数时,它会清除除id号和“_v”:0之外的数据,而不是更新对象 我已经与Postman一起检查了服务器端API,它可以用body内容类型x-form-urlencoded进行更新。我的直觉是在客户端正确获取数据。非常感谢您的帮助 以下是我的离子/角度控制器代码: .
.controller('UpdateCtrl', function($stateParams, $rootScope, $scope, HomeFac) {
id = $stateParams.id;
$scope.location = {};
HomeFac.getLocation(id).success(function(data) {
$scope.location = data;
});
$scope.edit = function() {
meet = $scope.location;
location = angular.fromJson(meet);
console.log(location);
HomeFac.updateLocation(id, location)
.then(function(id, location)
{
console.log("good");
});
};
$scope.delete = function() {
HomeFac.deleteLocation(id);
};
});
服务器端:
exports.putLocation = function(req, res) {
// Use the Beer model to find a specific beer
Location.findById(req.params.location_id, function(err, location) {
// Update the existing location
location.name = req.body.name;
location.category = req.body.category;
location.latitude = req.body.latitude;
location.longitude = req.body.longitude;
// Save the beer and check for errors
location.save(function(err, location) {
if (err) {
res.send(err)
};
res.json(location);
});
});
};
HomeFac更新功能
_LocationService.updateLocation = function(_id, location) {
return $http.put(urlBase + '/' + _id, location);
};
Mongoose返回的是“Mongoose文档”,而不是标准的JSON对象,因此如果您尝试更新的属性不在模式中,它们将不会添加到位置文档中 如果您所做的只是更新文档,那么不需要首先调用findById。您可以执行更新查询,因此传入一个_id以查找,然后传入一个更新对象。这将更新现有文档
exports.putLocation = function(req, res) {
var query = { _id: req.body.location_id };
var update = {
$set: {
name: req.body.name,
category: req.body.category,
latitude: req.body.latitude,
longitude: req.body.longitude
}
};
Location.update(query, update, function (err, location) {
if (err) {
return res.send(err);
}
res.json(location);
});
};
刚刚想出了解决办法。我必须修改客户端和服务器端的控制器。在服务器端,我使用findByIdAndUpdate进行操作。为了帮助那些可能遇到这个问题的人,我在下面发布了我的代码 服务器端:
exports.putLocation = function(req, res) {
// Use the Location model to find a specific location
Location.findByIdAndUpdate(req.params.location_id, req.body,
function(err, location) {
if (err)
res.send(err);
res.json({ data: location });
});
};
客户端控制器
.controller('UpdateCtrl', function($state, $stateParams, $rootScope, $scope, HomeFac) {
id = $stateParams.id;
$scope.location = {};
HomeFac.getLocation(id).success(function(data) {
$scope.location = data;
});
$scope.edit = function() {
name = $scope.location.name;
cat = $scope.location.category;
lat = $scope.location.latitude;
long = $scope.location.longitude;
mark = { name: name, category: cat, latitude: lat, longitude: long };
setmark = angular.toJson(mark);
HomeFac.updateLocation(id, setmark);
}
$scope.delete = function() {
HomeFac.deleteLocation(id);
$state.go('home');
}
});
您是否在中间件堆栈中启用了
bodyParser.json()
。对在我的server.js上,我有bodyParser.json()并添加了bodyParser.urlencoded({extended:true})。此外,在exports.putLocation
中,您只读取req.params
。如果将JSON放入服务器,req.params
将为空,数据将位于req.body
中。您必须考虑到这一点。如果(req.body)location=req.body,请尝试;else{location.name=req.params.name…}
当您使用x-form-urlencoded
发送数据时,数据位于req.params.
中,而使用application/json
时,数据位于req.body
中,只是尝试了一下。在邮递员身上它是有效的,但在我棱角分明的表格上它不是。可能是邮递员在做一个POST请求,而你的表格是PUT请求。我会玩一个帖子,或者尝试将req.body改为req.params,看看会发生什么。对不起,不太熟悉放