【问题标题】:Ionic/NodeJS/MEAN - Put clears object dataIonic/NodeJS/MEAN - Put 清除对象数据
【发布时间】:2015-12-07 17:36:05
【问题描述】:

我目前正在使用 Node/Express 和 Ionic/Angular 开发 MEAN 堆栈应用程序。我有一个页面,用户可以在其中编辑/删除特定对象的内容。删除功能有效,但是当我单击编辑并触发放置/更新功能时,它会清除除 id 号和 "__v": 0 之外的数据,而不是更新对象。

我已经使用 Postman 检查了服务器端 API,它可以使用正文内容类型 x-form-urlencoded 进行更新。我的预感是在客户端正确获取数据。任何帮助是极大的赞赏。

下面是我的 Ionic/Angular 控制器代码:

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

【问题讨论】:

  • 您是否在中间件堆栈中启用了bodyParser.json()
  • @PrashanthChandra。是的。在我的 server.js 上,我有 bodyParser.json() 并添加了 bodyParser.urlencoded({ extended: true })。
  • 另外,在exports.putLocation 中,您只是在阅读req.params。如果您将 JSON 放入服务器,req.params 将为空,数据将在req.body 中。你必须考虑到这一点。
  • 试试if (req.body) location = req.body; else {location.name = req.params.name...}
  • x-form-urlencoded发送数据时,数据在req.params.<property of object>,而application/json,数据在req.body

标签: javascript angularjs node.js ionic mean-stack


【解决方案1】:

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

【讨论】:

  • 刚刚试了一下。在 Postman 上它可以工作,但在我的 Angular 表单上却不行。
  • 可能是 Postman 正在执行 POST 请求,而您的表单是 PUT 请求。我会玩 POST 或尝试将 req.body 更改为 req.params 看看会发生什么。抱歉,对 PUT 不太熟悉
【解决方案2】:

刚刚想出了解决办法。我不得不在客户端和服务器端修改我的控制器。在服务器端,我使用 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'); 
   }

}); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 2015-07-23
    • 2017-12-10
    • 1970-01-01
    相关资源
    最近更新 更多