title: Writing a stream to gridfs-stream
body: <p>Been struggling with trying to get grid (mongodb) working in node, and either I am going about it the completely wrong way, or the grid system is not really that stable. Have tried about three different solutions, and now decided that I will try to use gridfs-stream (if no one knows a better solution.)</p>
  
  <p>And I think I have Almost gotten it working. But it just hangs on the file read (I think)</p>
  
  <pre><code>    var mongoose = require("mongoose");
      var fs = require("fs");
      var mongo = require('mongodb');
      var Grid = require('gridfs-stream');
  
  
      mongoose.connect('mongodb://localhost:27017/BabySounds');
  
  
      function loadFile(dataCollection, cuID, file){
          console.log("Will create id " + cuID + " in the collection " + dataCollection + " and store " + file);
          var gfs = new Grid(mongoose.connection.db, mongoose.mongo);
          var writeStream = gfs.createWriteStream({
              mode: 'w',
              _id: cuID
          });
          fs.createReadStream(file).pipe(writeStream);
          writeStream.on('close', function (filen) {
              console.log('Written file ' + filen.name);
          });
  
  
  
      }
  loadFile( 'fsSound', cuSoundID, srcSound); 
  </code></pre>
  
  <p>However, if you run the above code, it gets as far as creating the file, but (as far as I can tell) never gets to the close statement <code>writeStream.on('close',</code></p>
  
  <p>======= UPDATE =======</p>
  
  <p>Updated the code quite a bit to add as many tests as I could come to think of.</p>
  
  <pre><code>    var mongoose = require("mongoose");
  var fs = require("fs");
  var mongo = require('mongodb');
  var Grid = require('gridfs-stream');
  
  
  mongoose.connect('mongodb://localhost:27017/BabySounds');
  var db = mongoose.connection;
  
  
  function loadFile(dataCollection, cuID, file){
      console.log("Will create id " + cuID + " in the collection " + dataCollection + " and store " + file);
      var gfs = new Grid(mongoose.connection.db, mongoose.mongo);
      db.on('error', function(err){
          console.log('Got the following mongoose error: '+ err);
      });
      db.once('connected', function (condata) {
          console.log('The bloody database is open!');
          var writeStream = gfs.createWriteStream({
              mode: 'w',
              _id: cuID
          });
          console.log('And now lets write the thing');
          readStream = fs.createReadStream(file);
          readStream.pipe(writeStream);
          writeStream.on('data', function (chunk){
              console.log('Writing some data, just dont know what');
          });
          writeStream.on('end', function (filen) {
              console.log('Written file ' + filen.name);
          });
          writeStream.on('error', function (err) {
             console.log('Got the following error: ' + err);
          });
  
      });
  
  
  }
  </code></pre>
  
  <p>And I still get the same issue, it waits until the database is connected, there is no errors on the connection, but looking at the output (there is some other code that picks up the filename, sets the collection and generates the uID, and it does two in parallel, however, setting it to only do one makes no difference)</p>
  
  <pre><code>Will create id 553f39448c80bd9e7e6d904e in the collection fsImage and store ./data/kick.jpg
  Will create id 553f39448c80bd9e7e6d904f in the collection fsSound and store ./data/kick-808.wav
  The bloody database is open!
  And now lets write the thing
  The bloody database is open!
  And now lets write the thing
  </code></pre>
  
  <p>Still nothing past starting to write</p>