title: express/nodejs: stop duplicates in post request
body: <p>What should I do to stop the post request from posting new object if the object already exists?
  I have pasted my code and json object. I have tried <code>next();</code> but it doesnt work and a duplicate object with some other id is created no matter what. I am using <code>before</code>  beforePostTransaction function to check if the product already exists. The request contains the id and the store id of the </p>
  
  <p>index.js <br></p>
  
  <pre><code>const Transaction = require('./transactions/TransactionModel');
  const TransactionCTL = require('./transactions/TransactionController');
  Transaction.before('post', TransactionCTL.beforePostTransaction);
  Transaction.register(router, '/transactions');
  </code></pre>
  
  <p>TransactionController.js</p>
  
  <pre><code>const beforePostTransaction = (req, res, next) =&gt; {
  var id = req.body.id;
  Transaction.findById(
      id, 
      (err, data)=&gt;{
          if (!data){
              next();
          }
          else{
  
              var store = data.store;
              store = JSON.stringify(store);
              store = store.replace(/\"/g, "");
              if(store !== req.body.store){ //update the whole object
                  next();
              }
              else{
              //do what?
              }
  
          }
      });
      res.sendStatus(409);
  }
  </code></pre>
  
  <p>json object</p>
  
  <pre><code>[{
      "_id": "596db06849822a13c97ba3f9",
      "store": "596b088131ea400490897c50"
  }]
  </code></pre>