Node.js $ http PUT请求不向服务器返回任何内容

时间:2015-11-05 17:29:10

标签: angularjs node.js mongodb put meanjs

我正在开发一款管理客户信息的应用。我是MEAN堆栈的新手,不能为我的生活找出我在这里做错了什么。当我从视图中单击“更新客户”时,控制台会打印更新的信息,但是当我尝试将该信息发送到服务器并更新我的表时,我什么也得不到。客户实体与以前保持一致。

视图

  <div class="col-sm-12" id="addButton" style="padding: 10px;" >
<button 
  class="btn btn-primary" 
  ng-click="update()" 
  ng-disabled="editCustomerForm.$invalid">Update Customer</button>

控制器

    $scope.update = function(){
  console.log($scope.existingCustomer);
  $http.put('/customers/' + $scope.existingCustomer._id, $scope.existingCustomer).success(function(response) {
  refresh(); 
  $scope.existingCustomer={};
  $scope.editOldCustomer = false;
  })  
 };

server.js文件

app.put('/customers/:id', function (req, res) {
console.log(req.body);
var id = req.params.id
db.customers.findAndModify({
query: {_id: "mongojs.ObjectId(id)"},
update: {$set: {name: req.body.name, email: req.body.email, phone: req.body.phone,
                street: req.body.street, city: req.body.city, state: req.body.state, zip: req.body.zip}},
new: true}, function (err, doc) {
  res.json(doc);
}
 );
});

我一直在用这种方式震撼我的大脑几个小时,只是无法弄清楚我做错了什么。感谢您的帮助,我真的很感激。很抱歉,如果某些代码的格式在堆栈中搞砸了

1 个答案:

答案 0 :(得分:1)

你的问题看起来就在这里:query: {_id: "mongojs.ObjectId(id)"},你实际上并没有创建一个id,你可能应该这样做:var _id = mongojs.ObjectId(id);然后像这样使用它:{{ 1}}