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