title: how to dump csv data into mongoDB using angular and nodejs
body: <p>I've a csv file and want to dump the data in it into mongodb using angular and nodejs.</p>
  
  <p>Need help to read csv file's data using angular and parse it and store in mongodb</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import { Injectable } from '@angular/core';
  import { Http } from '@angular/http';
  import 'rxjs/add/operator/map';
  
  @Injectable()
  export class TemplateService {
  
    domain = 'http://localhost:3000';
    constructor(private http: Http) { }
  
  
    postCsvToMongo(csvJson: any) {
      console.log('CSV!!!' + csvJson);
      return this.http.post(this.domain + '/fu/fileupload', csvJson).map(res =&gt; res.json());
    }
  
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>this is the code i'm using to send json object over post routes.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
      &lt;head&gt;
          &lt;meta charset="utf-8"&gt;
          &lt;title&gt;Error&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;pre&gt;TypeError: Converting circular structure to JSON
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at JSON.stringify (&amp;lt;anonymous&amp;gt;)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at router.post (/home/agam/projects/AMEX/routes/fileUpload.js:10:23)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at Layer.handle [as handle_request] (/home/agam/projects/AMEX/node_modules/express/lib/router/layer.js:95:5)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at next (/home/agam/projects/AMEX/node_modules/express/lib/router/route.js:137:13)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at Route.dispatch (/home/agam/projects/AMEX/node_modules/express/lib/router/route.js:112:3)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at Layer.handle [as handle_request] (/home/agam/projects/AMEX/node_modules/express/lib/router/layer.js:95:5)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at /home/agam/projects/AMEX/node_modules/express/lib/router/index.js:281:22
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at Function.process_params (/home/agam/projects/AMEX/node_modules/express/lib/router/index.js:335:12)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at next (/home/agam/projects/AMEX/node_modules/express/lib/router/index.js:275:10)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at Function.handle (/home/agam/projects/AMEX/node_modules/express/lib/router/index.js:174:3)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at router (/home/agam/projects/AMEX/node_modules/express/lib/router/index.js:47:12)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at Layer.handle [as handle_request] (/home/agam/projects/AMEX/node_modules/express/lib/router/layer.js:95:5)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at trim_prefix (/home/agam/projects/AMEX/node_modules/express/lib/router/index.js:317:13)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at /home/agam/projects/AMEX/node_modules/express/lib/router/index.js:284:7
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at Function.process_params (/home/agam/projects/AMEX/node_modules/express/lib/router/index.js:335:12)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at next (/home/agam/projects/AMEX/node_modules/express/lib/router/index.js:275:10)
          &lt;/pre&gt;
      &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>this is the error i'm getting when I'm trying to debug the request received over the post request on node side.
  And how to receive the file over back end??</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var csv = require('fast-csv');
  var mongoose = require('mongoose');
  var Bulletin = require('../models/bulletin');
  var multer = require("multer");
  
  module.exports = (router) =&gt; {
  
      router.post('/fileupload', (req, res) =&gt; {
          console.log('@@@ ' + req);
          res.send(req.files);
      });
  
      /*router.post('/fileupload', multer({dest: "./uploads/"}).array("uploads", 12), function(req, res) {
          res.send(req.files);
      });*/
      return router;
  };</code></pre>
  </div>
  </div>
  </p>
  
  <p>This is the nodejs code</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>1","Fed official says weak data caused by weather, should not slow taper","http://www.latimes.com/business/money/la-fi-mo-federal-reserve-plosser-stimulus-economy-20140310,0,1312750.story\?track=rss","Los Angeles Times","b","www.latimes.com","1394470370698
  2","Fed's Charles Plosser sees high bar for change in pace of tapering","http://www.livemint.com/Politics/H2EvwJSK2VE6OF7iK1g3PP/Feds-Charles-Plosser-sees-high-bar-for-change-in-pace-of-ta.html","Livemint","b","www.livemint.com","1394470371207
  3","US open: Stocks fall after Fed official hints at accelerated tapering","http://www.ifamagazine.com/news/us-open-stocks-fall-after-fed-official-hints-at-accelerated-tapering-294436","IFA Magazine","b","www.ifamagazine.com","1394470371550
  4","Fed risks falling 'behind the curve', Charles Plosser says","http://www.ifamagazine.com/news/fed-risks-falling-behind-the-curve-charles-plosser-says-294430","IFA Magazine","b","www.ifamagazine.com","1394470371793
  5","Fed's Plosser: Nasty Weather Has Curbed Job Growth","http://www.moneynews.com/Economy/federal-reserve-charles-plosser-weather-job-growth/2014/03/10/id/557011","Moneynews","b","www.moneynews.com","1394470372027
  6","Plosser: Fed May Have to Accelerate Tapering Pace","http://www.nasdaq.com/article/plosser-fed-may-have-to-accelerate-tapering-pace-20140310-00371","NASDAQ","b","www.nasdaq.com","1394470372212</code></pre>
  </div>
  </div>
  </p>
  
  <p>This is my sample csv file</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&gt; amex@1.0.0 start /home/agam/projects/AMEX
  &gt; node index.js
  
  (node:6478) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
  listening on port 3000
  Conected to database: amex
  @@@ [object Object]</code></pre>
  </div>
  </div>
  </p>
  
  <p>this is the thing it is printing</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>// 1) Express for nodejs
  const express = require('express');
  const app = express();
  
  const template = require('./routes/template');
  const upload = require('./routes/upload');
  const path = require('path');
  // const fileUpload = require('express-fileupload');
  
  const router = express.Router();
  const fu = require('./routes/fileUpload')(router);
  
  //const carts = require('./routes/carts')(router);
  
  // 5) body parser
  const bodyParser = require('body-parser');
  
  const cors = require('cors'); // CORS is a node.js package for providing a Connect/Express middleware that can be used to enable CORS with various options.
  
  // 2) Mongodb connection
  var mongoose = require('mongoose');
  const config = require('./config/database');
  mongoose.connect(config.uri, (err) =&gt; {
      if (err) {
          console.log('error ' + err);
      } else {
          console.log('Conected to database: ' + config.db);
      }
  });
  
  app.use(cors({ origin: 'http://localhost:4200' }));
  // 5) put bodyparser code before the routes (converts request to req.body)
  app.use(bodyParser.urlencoded({ extended: true }));
  app.use(bodyParser.json());
  
  
  app.use(function(req, res, next) {
      res.header("Access-Control-Allow-Origin", "*");
      res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
      res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
      next();
    });
  
  // 3) connect node.js to angular 
  app.use(express.static(__dirname + '/client/dist/'));
  
  // app.use(fileUpload());
  
  app.use('/fu', fu); // Use Product routes in application
  //app.use('/carts', carts);
  // 
  
  
  
  app.get('*', (req, res) =&gt; {
      res.sendFile(path.join(__dirname + '/client/dist/index.html'));
  });
  
  
  
  app.listen(3000, () =&gt; {
      console.log('listening on port 3000');
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>this is my index.js file which you are expecting</p>