繁体   English   中英

在PUT方法nodejs中设置ID

[英]Set ID in PUT method nodejs

服务器上有代码

apiRoutes.put('/intake', function(req, res)  {
  Intake.findById({id, function(err, intake) {
      if (err)
          res.send(err);
            check : true;
            intake.save(function(err) {
      if (err) {
        return res.json({success: false, msg: 'Error'});
      }
      res.json({success: true, msg: 'Successful update check state.'});
    });
  }})
});

我应该从前端设置ID值,但是我不知道如何在函数中设置它。 试试这个apiRoutes.put('/ intake',id,function(req,res),但是id在controller.js的前面没有定义:

$scope.changeCheck = function(id) {
    console.log(id);
    mService.intake("PUT", $scope.intake, {"action": "put"}, id)
      .success(function(data, status, headers, config) {
    }).error(function(err) {
      mService.errorHandler(status);
    });
  };

并在服务文件中:

 intake : function(method, data, params, value) {

      var endpoint = "";
        switch (params.action) {
        case "put" :
          endpoint = "intake/" + value;
          break;
      }

      return this.request(method, endpoint, data);

    }

html

<li ng-repeat="intake in intakes">
                    <div class="welcome-box">
                        <div class="welcome-box-content" >
                        <label class="checkbox">
                        <input type="checkbox" ng-model="intake.check" ng-change="changeCheck(intake.pres_id)" />
                        </label>
                        <span class="drugs"> {{intake.dname}} <br></span> <span class="drugsdescr"><i class="fa fa-comment" aria-hidden="true"> </i> {{intake.comment}} <i class="fa fa-medkit" aria-hidden="true"></i> {{intake.dose1}}{{intake.dose2}} </span>
                        </div>
                    </div>
                </li>

获取摄入量

mService.intake("GET", "", {"action" : "get"})
  .success(function(data, status, headers, config) {
    $scope.intakes = data;
    console.log(data);
  })
  .error(function(data, status, headers, config) {
    mService.errorHandler(status);
  });

如果我没记错的话,您将在角度服务中提供id值作为url的一部分:

endpoint = "intake/" + value;

最终是这样的: intake/12345

由于此处未使用查询参数,因此服务器会将其作为url的一部分。

因此,您必须在服务器上将其指定为id是url的一部分:

'/ intake / :id '

apiRoutes.put('/intake/:id', function(req, res)  {
   ...
});

然后您可以从请求中获取id值:

req.params.id

因此,服务器上的put函数应如下所示:

apiRoutes.put('/intake/:id', function(req, res)  {
  var id = req.params.id;      

  Intake.findById({id, function(err, intake) {
      if (err)
          res.send(err);
            check : true;
            intake.save(function(err) {
      if (err) {
        return res.json({success: false, msg: 'Error'});
      }
      res.json({success: true, msg: 'Successful update check state.'});
    });
  }})
});

暂无
暂无

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

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