I am running express 4 using multer, gridfs-stream and mongoose with mongodb and I am attempting to upload a file and stream it to gridfs.
The express route that does this is defined as:
app.post('/uploadfile', function (req, res) {
console.dir(req.files);
// The mongodb instance created when the mongoose.connection is opened
var db = mongoose.connection.db;
// The native mongo driver which is used by mongoose
var mongoDriver = mongoose.mongo;
// Create a gridfs-stream
var gfs = new Gridfs(db, mongoDriver);
var file = req.files.myFile;
var fileId = new ObjectId();
console.log("Creating WriteStream");
var writeStream = gfs.createWriteStream({
_id: fileId,
filename: file.originalname,
mode: 'w',
content_type: file.mimetype,
metadata: {
id: '123',
number: '2',
name: "Kenny Erasmuson"
}
});
console.log("Created WriteStream");
req.pipe(writeStream);
console.log("Finished!");
});
Once the express app is running, a file is selected and uploaded (via an HTML multipart/form-data form), and the output from the node server is:
$ node server.js
Listening on port 8001
{ myFile:
{ fieldname: 'myFile',
originalname: 'kenny-credit-rating.pdf',
name: '1082e5071ede1002c4ae5be6123226d8.pdf',
encoding: '7bit',
mimetype: 'application/pdf',
path: 'uploads/1082e5071ede1002c4ae5be6123226d8.pdf',
extension: 'pdf',
size: 110782,
truncated: false,
buffer: null }
}
Creating WriteStream
Created WriteStream
Finished!
/Users/kenny/Dropbox/Dev/fileUploader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/co nnection/base.js:246
throw message;
^
MongoError: The dollar ($) prefixed field '$conditionalHandlers' in '_id.$conditionalHandlers' is not valid for storage.
at Object.toError (/Users/kenny/Dropbox/Dev/fileUploader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/utils.js:114:11)
at /Users/kenny/Dropbox/Dev/fileUploader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/collection/core.js:569:27
at /Users/kenny/Dropbox/Dev/fileUploader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1157:7
at /Users/kenny/Dropbox/Dev/fileUploader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1890:9
at Server.Base._callHandler (/Users/kenny/Dropbox/Dev/fileUploader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:448:41)
at /Users/kenny/Dropbox/Dev/fileUploader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:481:18
at MongoReply.parseBody (/Users/kenny/Dropbox/Dev/fileUploader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
at null.<anonymous> (/Users/kenny/Dropbox/Dev/fileUploader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:439:20)
at emit (events.js:95:17)
at null.<anonymous> (/Users/kenny/Dropbox/Dev/fileUploader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:201:13)
Has anyone any ideas what is causing the error and how to fix?
In my code:
I am assigning an ObjectId rather than the string representation of an ObjectId to the _id property of the object passed into gfs.createWriteStream. It turns out this is causing the MongoError in my code.
The fix, discovered here, is to change the line of code causing the issue to be:
Having done that I then came to the issue of the file not being put into the fs.chunks mongodb collection although the metadata does end up in the fs.files mongodb collection. AddieD's answer here starts to address this :)