I've a csv file and want to dump the data in it into mongodb using angular and nodejs.
Need help to read csv file's data using angular and parse it and store in mongodb
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 => res.json());
}
}
this is the code i'm using to send json object over post routes.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>TypeError: Converting circular structure to JSON
<br> at JSON.stringify (<anonymous>)
<br> at router.post (/home/agam/projects/AMEX/routes/fileUpload.js:10:23)
<br> at Layer.handle [as handle_request] (/home/agam/projects/AMEX/node_modules/express/lib/router/layer.js:95:5)
<br> at next (/home/agam/projects/AMEX/node_modules/express/lib/router/route.js:137:13)
<br> at Route.dispatch (/home/agam/projects/AMEX/node_modules/express/lib/router/route.js:112:3)
<br> at Layer.handle [as handle_request] (/home/agam/projects/AMEX/node_modules/express/lib/router/layer.js:95:5)
<br> at /home/agam/projects/AMEX/node_modules/express/lib/router/index.js:281:22
<br> at Function.process_params (/home/agam/projects/AMEX/node_modules/express/lib/router/index.js:335:12)
<br> at next (/home/agam/projects/AMEX/node_modules/express/lib/router/index.js:275:10)
<br> at Function.handle (/home/agam/projects/AMEX/node_modules/express/lib/router/index.js:174:3)
<br> at router (/home/agam/projects/AMEX/node_modules/express/lib/router/index.js:47:12)
<br> at Layer.handle [as handle_request] (/home/agam/projects/AMEX/node_modules/express/lib/router/layer.js:95:5)
<br> at trim_prefix (/home/agam/projects/AMEX/node_modules/express/lib/router/index.js:317:13)
<br> at /home/agam/projects/AMEX/node_modules/express/lib/router/index.js:284:7
<br> at Function.process_params (/home/agam/projects/AMEX/node_modules/express/lib/router/index.js:335:12)
<br> at next (/home/agam/projects/AMEX/node_modules/express/lib/router/index.js:275:10)
</pre>
</body>
</html>
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??
var csv = require('fast-csv');
var mongoose = require('mongoose');
var Bulletin = require('../models/bulletin');
var multer = require("multer");
module.exports = (router) => {
router.post('/fileupload', (req, res) => {
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;
};
This is the nodejs 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
This is my sample csv file
> amex@1.0.0 start /home/agam/projects/AMEX
> 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]
this is the thing it is printing
// 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) => {
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) => {
res.sendFile(path.join(__dirname + '/client/dist/index.html'));
});
app.listen(3000, () => {
console.log('listening on port 3000');
});
this is my index.js file which you are expecting