aheckmann / gridfs-stream

Easily stream files to and from MongoDB
MIT License
615 stars 120 forks source link

Getting TypeError: undefined is not a function on ReadStream #2

Closed jrgleason closed 12 years ago

jrgleason commented 12 years ago

The code looks as follows...

PlaylistProvider.prototype.streamFile = function(res, filename){
  console.log(filename)
  console.log(gfs)
  console.log("*")
  var readstream = gfs.createReadStream(filename);
  console.log("!")
  readstream.pipe(res);
}

I see the following in the db..

{ "_id" : ObjectId("4fe37c5264251d8f35000005"), "filename" : "06 - Fire Island (Phases).mp3", "contentType" : "audio/mpeg3", "length" : 12613659, "chunkSize" : 4096, "uploadDate" : ISODate("2012-06-21T19:56:05.660Z"), "aliases" : null, "metadata" : { "author" : "Jackie" }, "md5" : "394e7a00566ee8216b9b3e8db4f3f81b" }

But when I run this I get...

The name is 06 - Fire Island (Phases).mp3
{ db: 
   { databaseName: 'myRadio',
     serverConfig: 
      { host: 'localhost',
        port: 27017,
        options: [Object],
        internalMaster: true,
        connected: true,
        poolSize: 1,
        ssl: false,
        slaveOk: undefined,
        _used: true,
        _readPreference: null,
        socketOptions: [Object],
        logger: [Object],
        eventHandlers: [Object],
        _serverState: 'connected',
        _state: [Object],
        recordQueryStats: false,
        dbInstance: [Circular],
        dbInstances: [Object],
        connectionPool: [Object],
        isMasterDoc: [Object] },
     options: {},
     _applicationClosed: false,
     native_parser: undefined,
     bsonLib: 
      { Code: [Function: Code],
        Symbol: [Function: Symbol],
        BSON: [Object],
        DBRef: [Function: DBRef],
        Binary: [Object],
        ObjectID: [Object],
        Long: [Object],
        Timestamp: [Object],
        Double: [Function: Double],
        MinKey: [Function: MinKey],
        MaxKey: [Function: MaxKey] },
     bson: {},
     bson_deserializer: 
      { Code: [Function: Code],
        Symbol: [Function: Symbol],
        BSON: [Object],
        DBRef: [Function: DBRef],
        Binary: [Object],
        ObjectID: [Object],
        Long: [Object],
        Timestamp: [Object],
        Double: [Function: Double],
        MinKey: [Function: MinKey],
        MaxKey: [Function: MaxKey] },
     bson_serializer: 
      { Code: [Function: Code],
        Symbol: [Function: Symbol],
        BSON: [Object],
        DBRef: [Function: DBRef],
        Binary: [Object],
        ObjectID: [Object],
        Long: [Object],
        Timestamp: [Object],
        Double: [Function: Double],
        MinKey: [Function: MinKey],
        MaxKey: [Function: MaxKey] },
     _state: 'connected',
     pkFactory: 
      { [Function: ObjectID]
        index: 4,
        createPk: [Function: createPk],
        createFromTime: [Function: createFromTime],
        createFromHexString: [Function: createFromHexString] },
     forceServerObjectId: false,
     strict: false,
     notReplied: {},
     isInitializing: true,
     auths: [ [Object] ],
     openCalled: true,
     commands: [],
     _callBackStore: { _notReplied: {}, _events: [Object] },
     logger: { error: [Function], log: [Function], debug: [Function] },
     slaveOk: false,
     tag: 1340309301093,
     eventHandlers: 
      { error: [],
        parseError: [],
        poolReady: [],
        message: [],
        close: [] },
     serializeFunctions: false,
     raw: false,
     recordQueryStats: false,
     reaperEnabled: false,
     _lastReaperTimestamp: 1340309301093,
     retryMiliSeconds: 5000,
     numberOfRetries: 5,
     reaperInterval: 10000,
     reaperTimeout: 30000 },
  mongo: 
   { [Function: Db]
     super_: [Function: EventEmitter],
     DEFAULT_URL: 'mongodb://localhost:27017/default',
     connect: [Function] } }
*
TypeError: undefined is not a function
    at Stream.GridReadStream (/home/jackie/Development/Code/personal/nodejs/playlist/node_modules/gridfs-stream/lib/readstream.js:51:65)
    at Grid.module.exports.Grid.createReadStream (/home/jackie/Development/Code/personal/nodejs/playlist/node_modules/gridfs-stream/lib/index.js:26:12)
    at PlaylistProvider.streamFile (/home/jackie/Development/Code/personal/nodejs/playlist/playlist-mongo.js:231:24)
    at exports.streamFile (/home/jackie/Development/Code/personal/nodejs/playlist/routes/index.js:80:20)
    at callbacks (/home/jackie/Development/Code/personal/nodejs/playlist/node_modules/express/lib/router/index.js:171:11)
    at param (/home/jackie/Development/Code/personal/nodejs/playlist/node_modules/express/lib/router/index.js:145:11)
    at pass (/home/jackie/Development/Code/personal/nodejs/playlist/node_modules/express/lib/router/index.js:152:5)
    at Router._dispatch (/home/jackie/Development/Code/personal/nodejs/playlist/node_modules/express/lib/router/index.js:179:5)
    at Object.router (/home/jackie/Development/Code/personal/nodejs/playlist/node_modules/express/lib/router/index.js:39:10)
    at next (/home/jackie/Development/Code/personal/nodejs/playlist/node_modules/express/node_modules/connect/lib/proto.js:190:15)

This seems to be exactly the same as the example, my package.json says my mongo is 1.0.2 but when I try to upgrade it, the 2.0.5 version is not it the main repos.

aheckmann commented 12 years ago

hmm, it looks like the mongodb-native module isn't being passed correctly. how are you passing it in?

jrgleason commented 12 years ago

var Db = require('mongodb').Db; var Connection = require('mongodb').Connection; var Server = require('mongodb').Server; var BSON = require('mongodb').BSON; var ObjectID = require('mongodb').ObjectID; var GridStore = require('mongodb').GridStore; var Grid = require('gridfs-stream')(Db); var gfs = null;

PlaylistProvider = function(){}; PlaylistProvider.prototype.dummyData = [];

PlaylistProvider = function(host, port) { this.db= new Db('myRadio', new Server(host, port, {auto_reconnect: true}, {})); gfs = Grid(this.db) this.db.open(function(err,db){ db.authenticate(dbuser,dbpassword, function(err, result) {}); }); };

jrgleason commented 12 years ago

Also... console.log(gfs.db.options) outputs.. {} like expected console.log(gfs.mongo) outputs { [Function: Db] super_: [Function: EventEmitter], DEFAULT_URL: 'mongodb://localhost:27017/default', connect: [Function] }

Even this seems to work... var test = new GridStore(gfs.db, filename, 'r',{})

aheckmann commented 12 years ago

ah, try this:

var Grid = require('gridfs-stream')(require('mongodb'))
jrgleason commented 12 years ago

That seems to have fixed it thanks!

aheckmann commented 12 years ago

:D