【问题标题】:Set ID in PUT method nodejs在 PUT 方法 nodejs 中设置 ID
【发布时间】:2016-06-11 22:38:13
【问题描述】:

在服务器上有代码

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

【问题讨论】:

  • 在你的控制器中调用 changeCheck 是什么?例如,它是按钮单击吗?你能展示那部分代码吗?
  • 您的控制器中有进气口列表吗?你也可以添加你的控制器吗?

标签: javascript html angularjs node.js


【解决方案1】:

如果我没记错的话,您在 Angular 服务中提供了 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.'});
    });
  }})
});

【讨论】:

  • 成功了!谢谢!但我无法获得摄入的 id 值。为什么?控制台中未定义
  • 从摄入量到底是什么意思?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-10
  • 2022-01-04
  • 2019-07-18
  • 2016-09-02
相关资源
最近更新 更多