jdarling / MongoMQ

MongoMQ is a messaging queue built on top of Node.js and MongoDB's tailable cursors.
MIT License
90 stars 20 forks source link

Error: Connection was destroyed by application #22

Closed cyberdanes closed 10 years ago

cyberdanes commented 10 years ago

Getting the error while trying to write file to db. Also, the callbacks for several methods were returning null values which were interfering with the app. Appended '1' to the names in order to make it work.

js file code:

var fs = require('fs');
var Db = require('mongodb').Db,
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    ReplSetServers = require('mongodb').ReplSetServers,
    ObjectID = require('mongodb').ObjectID,
    Binary = require('mongodb').Binary,
    GridStore = require('mongodb').GridStore,
    Grid = require('mongodb').Grid,
    Code = require('mongodb').Code,
    BSON = require('mongodb').pure().BSON,
    assert = require('assert');

var db = new Db('storedvideos', new Server('locahost', 27017));
// Establish connection to db
db.open(function(err, db1) {
  console.log("start");
  // Our file ID
  var fileId = new ObjectID();
console.log("id created");
  // Open a new file  
  var gridStore = new GridStore(db, fileId, 'w');
console.log("store created");
  // Read the filesize of file on disk (provide your own)
  var fileSize = fs.statSync('C:/CardBoard/localvideos/videos/movie.mp4').size;
  console.log("file size read");
  // Read the buffered data for comparision reasons
  var data = fs.readFileSync('C:/CardBoard/localvideos/videos/movie.mp4');
console.log("file sync read");
  // Open the new file
  gridStore.open(function(err, gridStore1) {

    // Write the file to gridFS
    gridStore.writeFile('C:/CardBoard/localvideos/videos/movie.mp4', function(err, doc) {
      if(err) console.log(err);
      console.log(doc);
      // Read back all the written content and verify the correctness
      GridStore.read(db, fileId, function(err, fileData) {
        assert.equal(data.toString('base64'), fileData.toString('base64'))
        assert.equal(fileSize, fileData.length);

        db.close();
      });
    });
  });
});
jdarling commented 10 years ago

Interesting... I haven't looked at MongoMQ with the latest version of Node and some things may have changed. I'll have to see if I can find some time to take a look.

What "names" did you append 1 to? Sorry but don't see it in your code above.

Also, what version of Node and Mongo are you using?

cyberdanes commented 10 years ago

I am using mongo2.6.1 and node 0.10.28

Appended 1 to db.open(function(err, db1) {

and gridStore.open(function(err, gridStore1) {

jdarling commented 10 years ago

On 2nd look, I think you may have the wrong project. That is all pure MongoDB stuff, nothing to do with MongoMQ itself.