I'm attempting to process file posted to an Express site by placing it into MongoDB's gridfs system, using the native driver.
The problem I'm experiencing is that the request's chunks have all been sent before the gridStore object is even ready. Placing the request.on('data'... handler inside the callback that executes once the gridStore is ready doesn't work, either. There, it appears that all the chunks are sent prior to the callback setting up the handler to catch them. This is an obviously incomplete example ( I never see "chunk" logged to the console):
var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
ObjectID = require('mongodb').ObjectID,
GridStore = require('mongodb').GridStore,
db;
db = new Db('dbName', new Server('serverAddress', 27017), { safe: false });
db.open(function (err, db) {
console.log("db open");
var gridStore = new GridStore(db, new ObjectID(), 'fileName', "w");
gridStore.open(function (err, gridStore) {
console.log("gridStore open");
req.on('data', function (chunk) {
console.log("chunk");
gridStore.write(chunk, function () {});
});
});
})
I'm wondering if there is a way, from within an Express site's routes, to stop the stream from coming, until the gridStore is ready. Alternately, I would expect a synchronous way to set up a gridStore to work, if one exists.