title: mongoose how to save multiple collections in single route
body: <p>I want to save multiple collections in single express route. I am using below code in my express route file.</p>
  
  <pre><code>const userinfo = require('../models/userinfo');
  const userdtls = require('../models/userdtls');
  
  router.post('/add', function(req, res) {
    mongoose.connect(config.DB,{ useNewUrlParser: true }, function (err) { 
      if (err) throw err;     
      let newuserdtls = new userdtls({
        userid: '1001',
        username: req.body.username      
      })  
      newuserdtls.save()
        .then(result =&gt; {
             res.json(result);         
             console.log('successfully inserted');
        })
        .catch(err =&gt; {
          res.json(err)
          console.log('inserted failed',err);
        })      
      let newuserinfo = new userinfo({
        userid: '1001',
        username: req.body.username,
        gender: req.body.gender,
        class: req.body.class,
        status: req.body.status   
      })
      newuserinfo.save()
        .then(result =&gt; {
             res.json(result);          
             console.log('successfully inserted');
        })
        .catch(err =&gt; {
          res.json(err)
          console.log('inserted failed',err);
        })      
    })
  })
  </code></pre>
  
  <p>I want to insert into these two models as single transaction. Both should be inserted successfully. If one fails other also should fail. I am also getting <strong>"ERR_HTTP_HEADERS_SENT: Cannot set headers after they are sent to the client"</strong> using this code but that may be because of res.send twice. Please let me know how to combine two saves &amp; response into one. </p>
  
  <p>Thanks </p>