In reference to this post:
Node.js File Upload (Express 4, MongoDB, GridFS, GridFS-Stream)
I'm doing essentially what appears to be everything I should to save a file upload to Mongo GridFS. However, it does not appear that anything is being saved in the Mongo GridFS databases (no chunks or files). I'm using busboy to parse the multipart data coming in, I have verified that I'm connecting to the Mongo database through Mongoose... I'm just looking for a review to see why nothing is being saved!
router.route('/doc')
.post(function(req, resp, next) {
var gfs = gridfs(mongoose.connection);
var bb = new busboy({
headers: req.headers
});
bb.on('file', function(fieldname, file, filename, encoding, mimetype) {
var doc = new DocumentMeta();
doc.setFileName(filename);
doc.setMimetype(mimetype);
doc.setEncoding(encoding);
doc.insert(function(err) {
if(!err) {
var ws = gfs.createWriteStream({
_id: mongoose.Types.ObjectId().toString(),
filename: filename,
mode: 'w',
content_type: mimetype
});
ws.on('close', function(file) {
console.log('Doc Written to DB');
});
file.on('data', function(data) {
console.log('GOT DATA');
});
file.pipe(ws);
} else {
resp.json({success:false, msg: 'Error Creating Doc', data : { error : err.message }});
}
});
});
bb.on('finish', function() {
resp.json({success:true, msg: 'Successfully Uploaded Doc'});
});
req.pipe(bb);
});
I get the following console output:
Mongoose: fs.files.ensureIndex([ [ 'filename', 1 ] ]) { w: 1 }
GOT DATA
Mongoose: fs.chunks.ensureIndex([ [ 'files_id', 1 ], [ 'n', 1 ] ]) { w: 1 }
Mongoose: fs.files.findOne({ _id: ObjectId("56a16a383f6fbc877550752f") }) { w: 1, readPreference: 'primary' }
Mongoose: fs.chunks.remove({ files_id: ObjectId("56a16a383f6fbc877550752f") }) { w: 1, readPreference: 'primary' }
GOT DATA
GOT DATA
GOT DATA
GOT DATA
GOT DATA
POST /doc 200 756.218 ms - 50
I noticed that there is a call by mongoose to fs.chunks.remove... Why would it try to remove chunks? Also "close" is never called on the GridFS write stream, so I never output "Doc Written to DB" as expected...
Any guidance would be much appreciated! Thanks!