Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

In a mongodb from javascript code I am able to insert a file(of any extension) using gridfs, It's stored as fs.chunks & fs.files. Now i want to read this file's stream, but on any of the read opration calls on gridStore its giving error : "this.data.buffer.slice is not a function". I have used mongojs library from Node.JS.

// Write a file into MongoDB - Working fine.
// Our file ID
var fileId = new ObjectID();
 // Open a new file
var gridStore;
// Create a file and open it
gridStore = new GridStore(db, fileId, "D:/Files/Sofa 2 N050210.3DS", "w+");
var data = fs.readFileSync('D:/Files/Sofa 2 N050210.3DS');
gridStore.open(function (err, gridStore) {

gridStore.write(data, function (err, gridStore) {
// Flush the file to GridFS
gridStore.close(function (err, fileData) {
       // Write successfull
  });
 });
});

Now problem is when reading, After Closing gridStore, I tried to open file for reading & it is giving error at read call i.e on line no-4 in below code.

gridStore.close(function (result) {
var gridStore = new GridStore(db, "test_gs_seek_with_buffer", "r");
gridStore.open(function (err, gridStore) {
gridStore.read(5, function (err, data) {
  if(err){
          console.log("Error"); return;
     }
  console.log(data.toString());   
 });
}); 
});

Please help me find solution or the way to read back the file stored in GridFS (From the javascript code not from the command prompt).

share|improve this question

This code is working fine for me. Try with this. Hope it works.

var mongoose = require('mongoose');
 var Grid = require('gridfs-stream');
mongoose.connection.once('open', function () {
gfs = new Grid(mongoose.connection.db, mongoose.mongo);//If you are using mongoose
 });

var db = new mongo.Db('yourDatabaseName', new mongo.Server("127.0.0.1", 27017));  //if you are using mongoDb directily                                        
var gfs = Grid(db, mongo);         
var rstream = gfs.createReadStream(filename);
var bufs = [];
rstream.on('data', function (chunk) {
    bufs.push(chunk);
}).on('error', function () {
    res.send();
})
.on('end', function () { // done

            var fbuf = Buffer.concat(bufs);

            var File = (fbuf.toString('base64'));

            res.send(File);

 });
share|improve this answer
    
Thanks for your valuable reply. I had tried this before. Still tried once again & Its throwing error : Uncaught TypeError : gfs.createReadStream is not a function. – Vishwas Mane Sep 24 '15 at 10:47
    
I have edited the answer along with dB connection. I have provided both mongoose connection and mongoDb connection. Try with the one you are using . Hope it works this time. – Mariya James Sep 25 '15 at 4:24
    
Thanks Mariya, Uncaught TypeError : gfs.createReadStream() is gone, but Its giving new error @ call : "rstream.on('data', function (chunk) {}" – Vishwas Mane Sep 28 '15 at 7:10
    
can you provide me more details of error? – Mariya James Sep 28 '15 at 7:45
    
Error : Uncaught TypeError: this.data.buffer.slice is not a function : @ ..\Node modules\mongodb\lib\utils.js:97. – Vishwas Mane Sep 28 '15 at 8:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.