File storage in mongodb using gridfs and mongoose

I have the following code

gridfs.js has the following code that writes byte files

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 pattern is defined below. The mongoose schema has the 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); 

Below is the server code that is being called.

 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 following code, I get an error

api Error TrackDocs: RangeError: the maximum size of the call stack has been exceeded, and nothing has been added to GridFS. However, if I download the same file again, it saves it to GridFS.

Note that there are two trackDocs.saves. I am not sure if this is causing a problem or something else.

I am new to NodeJS and Mongoose, so any help would be greatly appreciated.

Thanks Melra

+6
source share
1 answer

Not sure if this helps, but it has helped me in the past.

Unsurprisingly, different browsers have different call stack sizes. It is also not surprising that the method they use to determine the call stack is also changing. The various sizes of the call stack that I could measure are (give or accept, can be disabled by 1 or 2):

We performed some tests ourselves to get call stack sizes in other browsers / hosts / OS 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/

Saying the option can pass --max-stack-size to node.

Set maximum v8 packet 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.

0
source

Source: https://habr.com/ru/post/956273/


All Articles