Read the file using node.js, mongoose, gridfs-stream

I use mongoose and gridfs-stream to store and read files with mongodb. I follow the example here: https://github.com/aheckmann/gridfs-stream

Writing files to db works fine, but I ran into the problem of reading files.

What mongodb looks like (show collections)

fs.chunks fs.files 

What does the file index look like (db.fs.files.find ())

 { "_id" : ObjectId("5140392659851df70b000001"), "filename" : "cover", "contentType" : "binary/octet-stream", "length" : 85734, "chunkSize" : 262144, "uploadDate" : ISODate("2013-03-13T08:30:30.299Z"), "aliases" : null, "metadata" : null, "md5" : "4476b26067daa0677978ba501308a35d" } 

Then I use this code to get a file called "cover"

 ... var gfs = Grid(mongoose.connection.db, mongoose.mongo) var readstream = gfs.createReadStream('cover') 

An error has occurred:

 Error: cover does not exist at self.collection.self.fileId (/mypath/node_modules/mongoose/node_modules/mongodb/lib/mongodb/gridfs/gridstore.js:198:26) at Cursor.nextObject (/mypath/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:654:35) at Cursor.close (/mypath/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:960:5) at Cursor.nextObject (/mypath/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:654:17) at Cursor.nextObject.commandHandler (/mypath/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:631:14) at Db._executeQueryCommand (/mypath/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1702:5) at g (events.js:185:14) at EventEmitter.emit (events.js:115:20) at Server.Base._callHandler (/mypath/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:130:25) at Server.connect.connectionPool.on.server._serverState (/mypath/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:517:20) 

I searched for it and found some possible links:

https://github.com/mongodb/node-mongodb-native/issues/621

Why gridfs get doesn't work with file identifier (ObjectId) only by file name

+6
source share
1 answer

The sample code on GitHub was a bit misleading; I initially received the same error message that you made. In my case, this was due to the fact that I was trying to read the file before the write stream ended. I solved this by reading inside the event handler for "close" :

 var fs = require("fs"), mongo = require("mongodb"), Grid = require("gridfs-stream"), gridfs, writeStream, readStream, buffer = ""; mongo.MongoClient.connect("mongodb://localhost/gridfs_test", function (err, db) { "use strict"; gridfs = Grid(db, mongo); // write file writeStream = gridfs.createWriteStream({ filename: "test.txt" }); fs.createReadStream("test.txt").pipe(writeStream); // after the write is finished writeStream.on("close", function () { // read file, buffering data as we go readStream = gridfs.createReadStream({ filename: "test.txt" }); readStream.on("data", function (chunk) { buffer += chunk; }); // dump contents to console when complete readStream.on("end", function () { console.log("contents of file:\n\n", buffer); }); }); }); 
+11
source

All Articles