I am trying to use an upload program to upload my files. The code that I use is
app.post('/photos',loadUser, function(req, res) {
var post = new Post();
req.form.complete(function(err, fields, files) {
if(err) {
console.log(err);
next(err);
} else {
ins = fs.createReadStream(files.file.path);
ous = fs.createWriteStream(__dirname + '/public/uploads/photos/' + files.file.filename);
post.filename=files.file.filename;
post.file=files.file.path;
util.pump(ins, ous, function(err) {
if(err) {
next(err);
} else {
post.save(function(err,docs) {
req.flash('info', 'information Saved');
res.redirect('/photos');
});
}
});
}
});
});
When I remove loadUser method everything is working fine, but when I use the loadUser method it is giving me an error. The console information of the error is:
Error: parser error, 0 of 4344 bytes parsed
at IncomingForm.write (/home/darhamid/node_modules/formidable/lib/incoming_form.js:141:17)
at IncomingMessage.<anonymous> (/home/darhamid/node_modules/formidable/lib/incoming_form.js:91:12)
at IncomingMessage.emit (events.js:67:17)
at HTTPParser.onBody (http.js:121:23)
at Socket.ondata (http.js:1349:22)
at TCP.onread (net_uv.js:312:27)
The error is caused only when i use loadUser function, if i remove the loadUser Funciton everything is working fine.
I don’t know the reason behind this and am stuck. Can anyone help me please?
You are trying to perform database operation before everything, which is creating problems for you. Try the following code:
Good luck…