I'm trying to save a file into GridFS in node.js. The usecase is actually really straight forward: "upload a file to a restify app, save it into GridFS".
function put(req, res, next) {
var ws, filename;
var application = new Application(req.params);
if (req.files.cv) {
filename = application._id + '/' + req.files.cv.name;
ws = gfs.createWriteStream({
filename: filename,
content_type: req.files.cv.type
});
ws.on('error', function (error) {
console.log('could not save: ', error);
});
fs.createReadStream(req.files.cv.path).pipe(ws);
application.files.push(filename);
}
/* cut */
application.save(function (err, application) {
/* cut */
next();
});
}
The console tells me
could not save: [RangeError: Maximum call stack size exceeded]
and since this is fired from an event, I don't have a stacktrace. Anyway nothing is written into GridFS.
Since on my part there is no recursion, I thought it has to be on gridfs-stream or mongodb's side, and since those are tested, I tried to simply higher the --stack-size
, this resulted in a Segmentation Fault.
This is my environtment: https://github.com/semmypurewal/node-dev-bootstrap
I've updated all dependencies (mongodb, mongoose, gridfs-stream) to their latest version: did not help.
What helped:
mongoose require()
s mongodb, it creates it's own little instance. If you require()
your own mongodb, you somehow f*ck everything up. Don't do it.
require()
mongoose. mongodb
from your package.json
mongodb
from node_modules
.If for some reason you need a reference to mongodb
(for example for gridfs-stream
), mongoose is nice enough to share its own with you.
Before:
var mongoose = require('mongoose');
var Grid = require('gridfs-stream');
var mongo = require('mongodb'); // BAD BOY! BAD!
//...
var gfs = Grid(mongoose.connection.db, mongo);
After:
var mongoose = require('mongoose');
var Grid = require('gridfs-stream');
var mongo = mongoose.mongo; // I LIKE, SO NICE!
//...
var gfs = Grid(mongoose.connection.db, mongo);