PUT method in Node.js/MongoDB -
i'm having issues simple put method in node.js (mongodb collection.update). appreciated. @ point, i'm not getting error, empty response.
index.js:
app.put('/updatevalues/:collection/:entity', function(req, res) { var params = req.params; var entity = params.entity; var collection = params.collection; var value1 = req.body.value1; var value2 = req.body.value2; if (entity) { collectiondriver.updatevalues(collection, entity, value1, value2, function(error, objs) { if (error) { res.status(400).send(error); } else { res.status(200).send(objs); } }); } else { res.send(400, {error: 'bad url', url: req.url}); } });
collectiondriver.js:
collectiondriver.prototype.updatevalues = function(collectionname, namedoc, value1new, value2new, callback) { this.getcollection(collectionname, function(error, the_collection) { if (error) callback(error); else { the_collection.update( { name: namedoc }, { $set: { value1: value1new, value2: value2new }}, function( err, result ) { if ( err ) throw err; } ); } }); };
testing with:
$ curl -i -x put -h 'content-type: application/json' -d '{"value1":"1","value2":"1"}' http://localhost/updatevalues/collection/test
reference callback passing in within function. presently not. seem expeting modified document in response, need .findoneandupdate()
instead:
collectiondriver.prototype.updatevalues = function(collectionname, namedoc, value1new, value2new, callback) { this.getcollection(collectionname, function(error, the_collection) { if (error) callback(error); the_collection.findoneandupdate( // <-- new method { name: namedoc }, { $set: { value1: value1new, value2: value2new }}, { returnoriginal: false }, // <-- tells return modified document callback // <--- passes callback passed in ); }); });
Comments
Post a Comment