import csv file mongodb using node js - node.js

want to save csv file in mongodb with node js i used javascript to to add and fetch data but it get error
get('/import', function(req, res, next) {
var stocks1 = [];
var csvStream = csv()
.on("data", function(data){
// var errorCount = 0;
// var csvStream = csv.parse({strictColumnHandling: true, headers: true}) .on("data", function(data){
var item = new Stock({
Diamondcode: data[0],
Sortdesc: data[1],
Colour: data[2],
Size: data[3],
Meserment:data[4] ,
Price:data[5]
});
item.save(function(error){
console.log(item);
if(error){
throw error;
}
});
}).on("end", function(){
console.log(" End of file import");
});
// stream.pipe(csvStream);
res.json({success : "Data imported successfully.", status : 200});
}).get('/fetchdata', function(req, res, next) {
Stock.find({}, function(err, docs) {
if (!err){
res.json({success : "Updated Successfully", status : 200, data: docs});
} else {
throw err;
}
});

That is because you haven't given the path of your CSV. One traditional way I used in my node projects as
kept route file different and pass your csv file in form data and then access it as req.files.
Make sure you create a wrapper for your CSV conversion

Related

multiple file upload not working with formidable in node js

app.post('/upload', function (req, res) {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
try{
if (files.file.name != '') {
file_newname = dt.MD5(files.file.name + Date() + Math.random()) + '.jpg' + ;
var file_newpath = './tmp/' + file_newname;
fs.readFile(file_oldpath, function (err, data) {
// Write the file
fs.writeFile(file_newpath, data, function (err) {
console.log('File written!');
res.end(JSON.stringify({
message: 'file uploaded successfully'
}));
});
});
}
}catch (e) {
}
});
});
The single image upload is working perfectly.I tried the following code
var form = new formidable.IncomingForm();
files = [],
fields = [];
form.on('field', function(field, value) {
fields.push([field, value]);
})
form.on('file', function(field, file) {
console.log(file.name);
files.push([field, file]);
})
form.on('end', function() {
console.log('done');
//res.redirect('/forms');
});
form.parse(req);
But only a single image gets uploaded. i m using react in frontend. Node and express in backend.
I also tried multer. But that doesnt working
app.post('/getrast', upload.array('files'), function (req, res) {
res.json({data: req.files});
});
Use the multiple flag with the incoming form with true as value.
var form = new formidable.IncomingForm();
form.multiples = true; //use this while dealing with multiple files
files = [],
fields = [];
form.on('field', function(field, value) {
fields.push([field, value]);
})
form.on('file', function(field, file) {
fs.rename('add your logic here for renaming files'); // rename it here
console.log(file.name);
files.push([field, file]);
})
form.on('end', function() {
console.log('done');
//res.redirect('/forms');
});
form.parse(req);

"media type unrecognized" when uploading an image to Twitter using npm twit

I am working on an express node app that posts to twitter when a user inputs an image into a form. I am saving the image locally before uploading, which works. After I encode the file to base64, I try to upload the base64-encoded file to Twitter using twit's media/upload feature. When I do this, I get an error saying "media type unrecognized."
Here is my code:
app.post('/tweet', function(req, res){
var time = new Date().getTime()
let image = req.files.image
var imgpath = './images/img' + time + '.jpg'
image.mv(imgpath, function(err) {
if (err){
return res.status(500).send(err);
}
});
var b64content = fs.readFileSync(imgpath, { encoding: 'base64' })
T.post('media/upload', {media: b64content}, function(err, data, res) {
if (err) console.log(err);
console.log(data);
T.post('statuses/update', {status: 'posted picture at: ' + time, media_ids: [data.media_id_string]}, function(err, params, res) {
if (err) console.log(err);
console.log(params);
});
});
return res.redirect('/')
})
Thank you!
Got it!. I needed to put the T.post code in the brackets of image.mv's function
use postMediaChunked function
var filePath = '/absolute/path/to/file.png'
T.postMediaChunked({ file_path: filePath }, function (err, data, response) {
console.log(data)
})

Excel data to json using node

Here i have upload my excel file, now i need to convert it to json,but it's showing the following error:
TypeError: Cannot set property length of [object Object] which has only a getter
Here is my code:
//other module dependencies also given
var converter = require('xls-to-json')
var Data = {};
exports.fnup = function(req, res) {
var upfile = req.file;
var userid = req.body.userid || '0';
if (uploafile.fieldname !== 'myFile') {
return res.status(400).send({
message: 'Please upload '
});
}
converter({
input: './foldern1/' + upfile.originalname,
output: null
},
function(err, results) {
console.log("hheeelo");
if (err) {
console.log('Error:', err);
} else {
console.log("suvbb", results);
Data = _.filter(results, function(result) {});
}
});
};
_.filter only works with an Array, not an Object. results is an Object.

Nodejs library RequestJS undefined MULTER req.files.fileName.path when use ExpressJS routes

I got a problem with Multer, undefined req.files.path field
First, my Express.js route is:
routes.js
router.post('/', function(req, res, next){
// id, name, usersArray[], info, iconImg, headerImg
var dataObject = new MyMongooseDataObject();
// Receive data
dataObject.id = uuid.v4();
dataObject.name = req.body.name;
dataObject.usersArray = req.body.usersArray;
dataObject.info = req.body.info;
return someBindingWrapperToStoreTheFile.postFile(uuid.v4(), [req.files.iconImg.path.toString(), req.files.headerImg.path.toString()])
.then(function (postedFiles) {
dataObject.iconImg = postedFiles.body.payload.files[0].id;
dataObject.headerImg = postedFiles.body.payload.files[1].id;
//save dataObject after storing images and processing data
dataObject.save(function(savedDataObject){
next(success(req, 200, 'dataObject Saved ' + savedDataObject.id));
});
})
.catch(function(err){
console.log('FAILED: ', err.stack);
return next(failure(req, 500, err));
});
});
When I test my route with a separate small requestJS script, it works just fine:
HTTP rest api request test with requestJS
postDataObject.js
var request = require('request');
var fs = require('fs');
var uuid = require('uuid');
var formData = {
name: 'someName',
info: 'Some INFO and text description. ',
'usersArray[0][uuid]': uuid.v4().toString(),
'usersArray[1][uuid]': uuid.v4().toString(),
'usersArray[2][uuid]': uuid.v4().toString(),
// handle files
iconImg: fs.createReadStream('/var/tmp/img/iconImg.png'),
headerImg: fs.createReadStream('/var/tmp/img/headerImg.png')
};
request.post({url:'http://127.0.0.1:2233/api/postDataObject', formData: formData}, function (err, httpResponse, body) {
if (err) {
return console.error('failed:', err);
}
console.log('DataObject creation is successful! Server responded with:', body);
});
Now I'm writing the wrapper library for the fronted usage, when I use the same code in another context, it seems, that Multer handling req.files.headerImg.path is not working, it's undefined.
The code I use for wrapper library:
wrapper-lib.js
var request = require('request'),
URI = require('URIjs'),
fs = require('fs'),
uuid = require('uuid'),
path = require('path'),
Promise = require('bluebird'),
_ = require('lodash'),
DataObjectBindings.prototype.createDataObject = function (jsonRequestJSFormData) {
var self = this;
// this give the URL of the API to make requests to
return self.getAPIurlHelper().then(function (apiUrl) {
return new Promise(function (resolve, reject) {
request.post({url: apiUrl, form: jsonRequestJSFormData}, function (err, res, body) {
if (err) {
reject(err);
} else {
resolve({res: res, body: JSON.parse(body)});
};
}); //end of .post
}); // end of Promise
}); // end of getAPIurlHelper() function
}; // end of createDataObject function definition
And Finally I test the warpper with:
wrapper-test.js
// Instantiate Broker Client
var Wrapper = require('./wrapper-lib');
var wi; //wrapper instalnce
var ConnectApiRequestTracer = require('./connectApiRequestTracer');
var fs = require('fs');
ConnectApiRequestTracer().connect()
.then(function () {
wi = new Wrapper();
return wi.createDataObject(
uuid.v4(),
{
name: 'Some Cool Name',
info: 'Some nice description and info. ',
'usersArray[0][uuid]': uuid.v4().toString(),
'usersArray[1][uuid]': uuid.v4().toString(),
'usersArray[2][uuid]': uuid.v4().toString(),
iconImg: fs.createReadStream('/var/tmp/img/iconImg.png'),
headerImg: fs.createReadStream('/var/tmp/img/headerImg.png'),
);
})
.then(function (data) {
console.log('SUCCESS: ', data);
})
.catch(function (err) {
console.log('FAILED', err.stack);
});
When I run the test case wrapper-test.js, it throws me an error, that says the req.files.iconImg.path is undefined.
Any ideas what can be wrong?
try using :
req.file.iconImg.path
req.file.headerImg.path
instead of req.files.iconImg.path and req.files.headerImg.path

error in node.js upload photo program

i want to develope and application in node.js where i shud b able to upload a video in my page and store a link to that video in database(mongodb).when i click //to the link the vedio should get displayed.also i shud b able to display all the //video's uploaded in the page.I tried to code to upload phot
//new show photo code
app.get('/photos', function(req, res) {
photos.list(function(err, photo_list) {
res.render('photos/index', {locals : {
photos: photo_list
}});
});
});
app.get('/photos/new', function(req, res){
res.render('photos/new', {
locals: {
title: 'New File Upload'
}
});
});
app.post('/photos', function(req, res) {
req.setEncoding('binary');
var parser = multipart.parser();
parser.headers = req.headers;
var ws;
parser.onpartBegin = function(part) {
consol.log('inside begin');
ws = fs.createWriteStream(__dirname + '/static/upload/photos.' + part.filename)
ws.on('error', function(err) {
throw err;
});
};
parser.onData = function(data) {
ws.write(data);
};
parser.onPartEnd = function() {
ws.end();
parser.close();
console.log('file successfully uploaded');
res.redirect('/photos');
};
req.on('data', function(data) {
console.log('shud not go here');
parser.write(data);
});
});
//can any one send me the code for the same or else find were i am doing //wrong.....answer immediately required....
You should use formidable for file uploads in Node.js, it's a widely used library for such a thing.

Resources