0

and thanks in advance for any help you guys can give me. I am trying to use mongodb, mongoose, gridfs-strea, and express to store img files on mongodb. I do not want to store the file in a folder. I want gfs.createWriteStream to take the file directly from app.post from express. App.post is currently saving some strings through a schema model into mongodb. The form that routes to app.post contains strings and a input for img file.

I tried to do this(dnimgfront being the name of the input):

dnimgfront:req.pipe(gfs.createWriteStream('req.body.dnimgfront'))

I am getting back.

TypeError: Object function Grid(db, mongo) {
 if (!(this instanceof Grid)) {
   return new Grid(db, mongo);
 }

 mongo || (mongo = Grid.mongo ? Grid.mongo : undefined);

My problem is I have to be able to store the img and the strings from the same app.post save function. Any ideas?

What I'm using to connect to mongodb is:

mongoose.connect('mongodb://localhost/veeltaxi');

var con=mongoose.connection;

con.once('open', function(){
    console.log('connected to mongodb succesfully!')
});

Thanks in advance for your help.

jsrosas
  • 103
  • 2
  • 14

1 Answers1

0

First, you need an instead of gridfs-stream:

var GridStream = require('gridfs-stream');
var mongodb = require('mongodb');
var gfs = new GridStream(mongoose.connection,mongodb);

At which point you can create the write stream and pipe:

var writeStream = gfs.createWriteStream({
   filename: filename, // the name of the file
   content_type: mimetype, // somehow get mimetype from request,
   mode: 'w' // ovewrite
});

req.pipe(writeStream);

However, at this time the Node.js MongoDB Driver is at version 2.0 which uses Streams 2, so there is no reason to use gridfs-stream if you're using that version of the mongodb lib. See the source code for v2.0.13. FYI, the stream that is implemented in the updated driver is Duplex. (Aside: gridfs-stream has its share of problems, and while actively maintained, is not very frequent.)

zamnuts
  • 9,492
  • 3
  • 39
  • 46
  • can I call re.pipe(writeStream(req.body.dnimgfront)), meaning I pass the req.body that stores the img from the form to the writeStream? This would be within the app.post function. – jsrosas Jan 13 '15 at 20:05
  • @jsrosas you can call it inline like that, yes, but not the way you have it. are you using multipart? what express middleware are you using? etc? – zamnuts Jan 13 '15 at 20:08
  • body-parser with the method-override urlenconded enconded extended = true – jsrosas Jan 13 '15 at 20:09
  • I am developing in c9.io so far i've not had to require mongodb. it already comes installed do i still have to require? Thanks for your help so far zammnuts – jsrosas Jan 13 '15 at 20:13
  • @jsrosas `body-parser` doesn't handle streaming nor multi-part content IIRC, i would use something like [`multiparty`](https://www.npmjs.com/package/multiparty). I've never used c9.io, but gridfs-stream needs an instance of mongodb to initialize correctly, so make sure you get it somehow and pass it in. – zamnuts Jan 13 '15 at 20:15
  • I did a npm link to the global of npm so far that works. Multiparty I will have to research. Can multiparty replace completely body-parser? will i will need the method-override? – jsrosas Jan 13 '15 at 20:24