1

I am trying to read image from GridFS. And getting following error.

var mongoose = require('mongoose');

exports.getLogo = function (req, res, next) {    


    var conn = mongoose.createConnection('localhost', 'database-name', 27017);

    conn.once('open', function () {

        var gs = new mongoose.GridStore(conn.db, 'img1.png', "w");

        gs.read(function (err, data) {

            res.setHeader('Content-Type', gs.contentType);
            res.setHeader('Content-Length', gs.length);
            res.setHeader('Content-Disposition', 'inline; filename="' + gs.filename + '"');

            res.end(data);

        });
    });

};

Error :-

D:\Projects\node_modules\mongoose\node_modules\mongodb\lib\mongodb\db.js:279
          throw err;
                ^
TypeError: undefined is not a function

How to solve this error.

edited -

TypeError: Cannot call method 'length' of undefined
    at Stream.GridStore.read

gridfs-stream output :-

enter image description here

Anup
  • 9,396
  • 16
  • 74
  • 138
  • where do you the GridStore code? last time check GridStore is not part of mongoose. both conn and gs need to call open. read the native node.js mongodb doc – wayne Mar 06 '14 at 05:45
  • i am using gridFS...http://mongodb.github.io/node-mongodb-native/markdown-docs/gridfs.html – Anup Mar 06 '14 at 05:47
  • 1
    change var gs = new mongoose.mongo.GridStore(conn.db, 'img1.png', "r"); also read the doc "When GridStore object is created, it needs to be opened". – wayne Mar 06 '14 at 05:56
  • thanks..1st error got solved.....i am getting error now `TypeError: Cannot call method 'length' of undefined at Stream.GridStore.read` – Anup Mar 06 '14 at 06:04

1 Answers1

2

you really need to read the doc carefully.

var mongoose = require('mongoose');

exports.getLogo = function (req, res, next) {    


    var conn = mongoose.createConnection('localhost', 'database-name', 27017);

    conn.once('open', function () {

        var gs = new mongoose.mongo.GridStore(conn.db, /* the id */, 'img1.png', "r");
        gs.open(function(err, gs) {
            gs.read(gs.length, function (err, data) {

              res.setHeader('Content-Type', gs.contentType);
              res.setHeader('Content-Length', gs.length);
              // gs don't have a property filename, filename need to come from your req.
              res.setHeader('Content-Disposition', 'inline; filename="' + 'img1.png' + '"');

              res.end(data);

            });
        })



    });

};
wayne
  • 3,410
  • 19
  • 11
  • I used this code...it gives no error...But it doesn't send any response...I do have the specified file in my db... – Anup Mar 06 '14 at 06:16
  • In Advanced Rest Client...the progress bar keeps moving...! – Anup Mar 06 '14 at 06:17
  • did the read function callback executed and the data is not null? – wayne Mar 06 '14 at 06:24
  • what i need here is `binary data` of the `image`.....that is what i need to send as response.... i rechecked again....image is present in db...i also tried with another img...stil data shows `null`. – Anup Mar 06 '14 at 06:33
  • To open GridStore it require the ObjectId, var gs = new mongoose.mongo.GridStore(conn.db, /* the id */, 'img1.png', "r"); filename is optional. you can specify it in read. It is not very useful then. try this lib https://github.com/aheckmann/gridfs-stream – wayne Mar 06 '14 at 08:34
  • i am using the same library...it was not giving output so i tried for Gridstore...! – Anup Mar 06 '14 at 08:45
  • i posted a new question http://stackoverflow.com/questions/22219400/display-image-in-gridfs – Anup Mar 06 '14 at 08:52