简体   繁体   English

NodeJS /角度编辑请求出现400错误

[英]NodeJS/Angular Edit Request Gets 400 Error

I'm working on an application with Ionic/Angular with a NodeJS backend.I have made an update form where a user can edit/delete a row with. 我正在使用带有NodeJS后端的Ionic / Angular的应用程序。我制作了一个更新表单,用户可以在其中使用/编辑行 The delete works, but I am having trouble with the edit function. 删除有效,但是编辑功能遇到问题。 On my console, this gets displayed: http://localhost:3000/api/todos/%7B%22todo%22:%22NewT%22,%22todo_text%22:%22Text%20EditionT%22%7D . 在我的控制台上,将显示以下内容: http:// localhost:3000 / api / todos /%7B%22todo%22:%22NewT%22,%22todo_text%22:%22Text%20EditionT%22%7D I am wondering how would I make the edit request to work. 我想知道如何使编辑请求生效。

Below is my code for the server side request: 以下是我的服务器端请求代码:

router.put('/api/todos/:_id', function(req, res) {

db.todos.update({
  _id: mongojs.ObjectId(req.body._id)
}, {
  todo: req.body.todo, 
  todo_text: req.body.todo_text
}, {}, function(err, data) {
  res.json(data);
});

});

Code for my update form (both delete and edit function). 我的更新表单的代码(具有删除和编辑功能)。 Note that HomeFac is for my services.js, where it directs to the REST API ( http://localhost:3000/api/todos/(id number): 请注意,HomeFac适用于我的services.js,它指向REST API( http:// localhost:3000 / api / todos /(id号):

.controller('UpdateCtrl', function($stateParams, $rootScope, $scope, HomeFac) {  
id = $stateParams.id;  

$scope.todo = {}; 

HomeFac.getBeer(id).success(function(data) {  

      var met = data[0]; 

      $scope.todo.todo = met.todo; 
      $scope.todo.todo_text = met.todo_text; 
});

$scope.edit = function(id, inputs) {  

    var inputs = { 
        todo : $scope.todo.todo, 
        todo_text : $scope.todo.todo_text
     };  

    data = angular.toJson(inputs); 

    HomeFac.updateBeer($stateParams.id, data
    ).then(function(id, data) { 
      alert("edited!"); 
    }); 
}; 

$scope.delete = function() { 
   HomeFac.deleteBeer(id);
}; 

}); 

Beer Service in services.js services.js中的啤酒服务

_BeerService.updateBeer = function(todo, _id) { 
 return $http.put(urlBase + '/' + _id, todo); 
}; 

It's probably because req.body._id is undefined . 可能是因为req.body._id undefined You're probably looking for req.params._id . 您可能正在寻找req.params._id Given that it's undefined, the update function isn't able to find the correct document. 由于未定义,因此更新功能无法找到正确的文档。

Note: You don't have to make your url parameter _id . 注意:您不必设置url参数_id URL parameters are like function parameters: you could name them whatever you want, and so you probably want to name it something that is more convenient, like id . URL参数就像函数参数一样:您可以根据需要命名它们,因此您可能想为其命名更方便,例如id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM