javascript - Set ID in PUT method nodejs -


have code on server

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

i should set id value frontend, don't know how can set in function. try apiroutes.put('/intake', id, function(req, res), id not defined on front in 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);     });   }; 

and in services file:

 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> 

get intakes

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

if i'm not mistaken providing id value in angular service part of url:

endpoint = "intake/" + value; 

which ends being this: intake/12345

since not using query params here, server expect part of url.

so have specify on server id part of url:

'/intake/:id'

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

and can id value request:

req.params.id 

so put function on server should this:

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

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -