6

I have the following code

gridfs.js has the following code that writes file bytes

exports.putFile = function(path, name, options, fn) {
    var db;
    db = mongoose.connection.db;
    options = parse(options);
    options.metadata.filename = name;
    return new GridStore(db, name, "w", options).open(function(err, file) {
        if (err) {
            return fn(err);
        }
        return file.writeFile(path, fn);
    });
};

The mongoose schema is defined below. The mongoose schema has a file name and the file itself.

var fs = require('fs');
var mongoose = require('mongoose');
var db = mongoose.connection;
var gridfs = require('./gridfs');
var Schema = mongoose.Schema;

var trackDocsSchema = mongoose.Schema(
        {
            _id : Schema.Types.ObjectId,
            docName: 'string',
            files: [ mongoose.Schema.Mixed ]
        }
);

trackDocsSchema.methods.addFile = function(file, options, fn) {
    var trackDocs;
    trackDocs = this;
    return gridfs.putFile(file.path, file.name, options, function(err, result) {
        if (err) console.log("postDocsModel TrackDocs Error: " + err);

        trackDocs.files.push(result);
        return trackDocs.save(fn);
    });
};

var TrackDocs = mongoose.model("TrackDocs", trackDocsSchema);

The server code that is invoked is below.

exports.uploadFile = function (req, res) {
    console.log("uploadFile invoked");
          var trackDocs, opts;
        trackDocs = new TrackDocs();
        trackDocs._id = mongoose.Types.ObjectId(req.body._id);
        trackDocs.docName = req.body.docName;
        opts = {
            content_type: req.files.file.type
        };

        return trackDocs.addFile(req.files.file, opts, function (err, result) {
            if (err) console.log("api TrackDocs Error: " + err);

            console.log("Result: " + result);
            trackDocs.save();
            console.log("Inserted Doc Id: " + trackDocs._id);
            return res.json(true);
        });
    };
});

When I run the folowing code, I get an error

api TrackDocs Error: RangeError: Maximum call stack size exceeded and nothing is added to GridFS. However, if I upload the same file again, it does save it into GridFS.

Notice there are two trackDocs.saves. I am not sure if that is causing the issue or something else.

I am new to NodeJS and Mongoose so any assitance will be much appreciated.

Thanks, Melroy

Mel
  • 245
  • 5
  • 15

1 Answers1

0

Not sure whether this helps or not, but it has helped me in the past.

Not surprisingly, different browsers have different call stack sizes. Also not surprisingly, the method that they use to determine the call stack varies as well. The various call stack sizes I could measure are (give or take, might be off by 1 or 2):

We ran some tests ourselves in order to get the call stack sizes on other browsers/hosts/OSs using this simple code:

var i = 0;

function recurse () {
    i++;
    recurse();
}

try {
    recurse();
} catch (ex) {
    alert('i = ' + i + '\nerror: ' + ex);
}

http://javascriptrules.com/2009/06/30/limitation-on-call-stacks/

In saying that an option could be passing --max-stack-size to node.

Set max v8 stack size (bytes)

node --max-stack-size 32000 app.js

NOTE: Help prints it as --max-stack-size, in node -v v0.10.x+ you need to use --stack-size instead.

gotnull
  • 26,454
  • 22
  • 137
  • 203