title: How to get id back, after posting data to mongoDb?
body: <p>I am using node.js, angularjs, &amp; mongoDb. <br/>
  I am creating a product upload page.<br/><br/>
  It is divided into  2 parts:<br/></p>
  
  <ol>
  <li><strong>Data page</strong>: This part will contain, textfields &amp; dropdowns.</li>
  <li><strong>Image Upload page</strong>: This part will have image upload control.
  <br/></li>
  </ol>
  
  <p>So I thought to create 2 forms in same page, from first page i will post text data to mongoDb, return <strong>product_id</strong> of the newly created product, and then upload images with returned <strong>product_id</strong>.
  <br/><br/>
  I have developed restFul API to post product <code>api/products/create-product</code>.
  <br/>
  <strong>Product model</strong> :</p>
  
  <pre><code>{
  productName:{type: String},
  productPrice:{type: Number}
  }
  </code></pre>
  
  <p><br/>
  <strong>Image model</strong>:</p>
  
  <pre><code>{
  productId:{type: String},
  imagePaths:[{type: Array}]
  }
  </code></pre>
  
  <p><br/>
  <strong>Product Controller(Angular):</strong></p>
  
  <pre><code>$scope.newProduct = function(){
        var formData = new FormData;
        for(key in $scope.product){
          formData.append(key, $scope.product[key]);
        } 
    //getting the files
    var file = $('#file')[0].files[0];
    formData.append('image', file);
  
    //Post data
    $http.post('http://localhost:3000/products/api/new-product',formData,{
      transformRequest: angular.identity,
      headers: {'Content-Type': undefined}
    }).then(function(res){
      $scope.item = res.data;
  
    });
  }
  </code></pre>
  
  <p><br/>
  <strong>Angular front-end</strong></p>
  
  <pre><code>&lt;input type="text" class="form-control" ng-model="product.productName" placeholder="Enter Product Name"&gt;
  &lt;input type="file" multiple="multiple" id="file" &gt;
  &lt;button type="submit" ng-click="newProduct()" class="btn btn-primary"&gt;Add Product&lt;/button&gt;
  </code></pre>
  
  <p><br/>
  <strong>POST API</strong></p>
  
  <pre><code>router.post('/api/new-product',upload.any(),function(req, res, next){
  
    var pro = req.body;
    if(req.files){
      req.files.forEach(function(file){
  
        var filename = (new Date()).valueOf() + '-' + file.originalname;
        fs.rename(file.path,'public/images/'+ filename, function(err){
          if (err) throw err;
          //Save to mongoose
  
        var product = new Products({
            productName: req.body.productName
           });
            product.save(function(err, result){
            if(err){ throw err}
              res.json(result);
          });
  
  
  
        });
      });
    }
  });
  </code></pre>
  
  <p><strong>Questions</strong>:</p>
  
  <ol>
  <li>Am I doing this correct way, or there is another better way for doing this?</li>
  <li>If this is correct way, then how can I post get posted <strong>product_id</strong>, in order to post images?
  <br/>
  Thanks.</li>
  </ol>