0

Hi there I'm starting to Develop Restful API using Restify but I'm having problems developing [PUT].

My idea is to get the params and change it using the new params. But this code does not seem to work. Please help me out :D

server.put('/user/:_id', function (req, res, next) {
  var user = {};
  user._id = req.params._id;
  console.log('_id ===  '+user);
  var changes = req.params;
  delete changes._id;
  // delete changes._id;
  for(var x in changes) {
    user[x] = changes[x];
    console.log('user[x] ='+user[x]);
  }
  db.Student.update(req.params._id, user,{multi:true,upsert:true}, function(err,data){
  res.writeHead(200, {'Content-Type': 'application/json; charset=utf-8'});
  res.end(JSON.stringify(user));

  return next();
});
});

here is the putclient

var restify = require('restify');
var server = require('./app');

var client = restify.createJsonClient({
    url: 'http://localhost:3000'
});

var testUser = {
  $set:
  {
    "name" : "procopio magalpok",
    "yearLevel" : "IX"
  }
};

 testUser._id = "561de16f885fb2f40d23ece",

 client.put('/user/'+testUser._id, testUser , function (err, req, res, status) {
    if (err) {
        console.log("An error ocurred >>>>>>");
        console.log(err);
    } else {
        console.log('id : '+testUser._id);
        console.log('User updated >>>>>>>');
        console.log(status);
    }

});
Kenichi Shibata
  • 148
  • 2
  • 11

0 Answers0