Tuesday 16 May 2017

Is it possible to show the whole updated record instead of the updated field only?

So basically, I am creating an inventory management app and want to show the updated record to the user in the sense not just the field that was updated but the whole document itself. My question is

*How do I show the whole updated record instead of the updated field to the user ? *

I am using postman to test this api. This is my code and those with comments are what I have tried.

var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var url = 'mongodb://localhost:27017/myproject';

module.exports = {
/*getCollection : function(req, res){
    var issueQty = req.body.issueQty;
    var itemDescrip = req.body.itemDescrip;
    MongoClient.connect(url,function(err,db){
        assert.equal(null,err);
        var doc = findMultiple(db,req,function() {
            db.close();
        });
    });
        res.send(doc)
}*/
postCollection : function(req, res){
    var issueQty = req.body.issueQty;
    var itemDescrip = req.body.itemDescrip;
MongoClient.connect(url, function(err, db) {
    assert.equal(null, err);
    updateRecord(db, req, function() {
    db.close();
    });
    //findMultiple(db, req, function(){
    //db.close();
    //});
});
    res.send('Record Found. Now updating this document...' + itemDescrip + ' 
Record Updated. This is the new record with updated issueQty '
    + issueQty )
}
}
var updateRecord = function(db, req, callback) {
db.collection('documents').updateMany(
  { 'Item Description': req.body.itemDescrip},
  {
    $set: { 'Issued QTY': req.body.issueQty }
  }
  ,
  function(err, results) {
    console.log(results);
    console.log('Done');
    callback(); 
});
};
/*var findMultiple = function(db, req, callback){
 var issueQty = req.body.issueQty;
 var itemDescrip = req.body.itemDescrip;
 var cursor = db.collection('documents').find({'Issued QTY': issueQty, 'Item 
 Description': itemDescrip});
 cursor.each(function(err,doc){
     assert.equal(err,null);
     if(doc != err){
         console.log('Successfully queried');
         console.log(doc);
         return doc;
     } else {
        callback();
     }

 });
};*/

I have tried putting another function findMultiple to get the data and show the updated record in the response but it did not work and have thought about linking my query api and the update api together but am not sure how to go about it. Any help is appreciated, thanks!



via Ong Kong Tat

No comments:

Post a Comment