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 createWriteStream #80

Closed prcbass closed 9 years ago

prcbass commented 9 years ago

I'm using node.js to attempt to store an image in my mongodb. Having required the necessary modules:

//app.js
  var mongo = require('mongodb');
  var monk = require('monk');
  var db = monk('localhost:27017/test-api');

app.use(function(req,res,next){
     req.db = db;
     req.mongo = mongo;
     next();
});

I'm trying to write to a stream as such:

//index.js

var Grid = require('gridfs-stream');
var mongo = require('mongodb');
var multer = require('multer');

router.post('/createpost', cpUpload, function(req,res){
  var db = req.db;
  var gfs = Grid(db,mongo);
  console.log(gfs);

  var file = req.file;

  console.log("Creating writestream");
  var writestream = gfs.createWriteStream({
     filename: file.originalname,
     mode:'w',
     content_type: file.mimetype,
     metadata: req.body.postText
   });
  console.log("HELLO BEFORE READING");
  fs.createReadStream(file.path).pipe(writestream);

  console.log("WRITESTREAM ON FUNCTION");
  writestream.on('close', function(file){
     res.send("Succcess!");
     fs.unlink(file.path,function(err){
        if(err){
          console.log("Error: ", err);
          console.log("DELETED: ", file.path);
        }

     });
   });
 });

When I run this code I get the following in terminal:

{ db: 
    { driver: 
            { _construct_args: [],
             _native: [Object],
            _emitter: [Object],
            _state: 2,
            _connect_args: [Object] },
        helper: { toObjectID: [Function], isObjectID: [Function], id: [Object] },
        collections: { advertcollection: [Object] },
        options: { safe: true },
        _events: {} },
mongo: 
    { [Function]
            MongoError: { [Function: MongoError] create: [Function] },
            MongoClient: { [Function: MongoClient] connect: [Circular] },
            Db: 
                { [Function]
                super_: [Object],
                SYSTEM_NAMESPACE_COLLECTION: 'system.namespaces',
                SYSTEM_INDEX_COLLECTION: 'system.indexes',
                SYSTEM_PROFILE_COLLECTION: 'system.profile',
                SYSTEM_USER_COLLECTION: 'system.users',
                SYSTEM_COMMAND_COLLECTION: '$cmd',
                SYSTEM_JS_COLLECTION: 'system.js' },
 Collection: [Function],
 Server: { [Function] super_: [Object] },
 ReplSet: { [Function] super_: [Object] },
 Mongos: { [Function] super_: [Object] },
 ReadPreference: 
  { [Function]
    isValid: [Function],
    PRIMARY: 'primary',
    PRIMARY_PREFERRED: 'primaryPreferred',
    SECONDARY: 'secondary',
    SECONDARY_PREFERRED: 'secondaryPreferred',
    NEAREST: 'nearest' },
 GridStore: 
  { [Function: GridStore]
    DEFAULT_ROOT_COLLECTION: 'fs',
    DEFAULT_CONTENT_TYPE: 'binary/octet-stream',
    IO_SEEK_SET: 0,
    IO_SEEK_CUR: 1,
    IO_SEEK_END: 2,
    exist: [Function],
    list: [Function],
    read: [Function],
    readlines: [Function],
    unlink: [Function] },
 Chunk: { [Function] DEFAULT_CHUNK_SIZE: 261120 },
 Logger: 
  { [Function]
    reset: [Function],
    currentLogger: [Function],
    setCurrentLogger: [Function],
    filter: [Function],
    setLevel: [Function] },
 Cursor: { [Function] super_: [Object], INIT: 0, OPEN: 1, CLOSED: 2, GET_MORE: 3 },
 Binary: 
  { [Function: Binary]
    BUFFER_SIZE: 256,
    SUBTYPE_DEFAULT: 0,
    SUBTYPE_FUNCTION: 1,
    SUBTYPE_BYTE_ARRAY: 2,
    SUBTYPE_UUID_OLD: 3,
    SUBTYPE_UUID: 4,
    SUBTYPE_MD5: 5,
    SUBTYPE_USER_DEFINED: 128,
    Binary: [Circular] },
 Code: { [Function: Code] Code: [Circular] },
 DBRef: { [Function: DBRef] DBRef: [Circular] },
 Double: { [Function: Double] Double: [Circular] },
 Long: 
  { [Function: Long]
    fromInt: [Function],
    fromNumber: [Function],
    fromBits: [Function],
    fromString: [Function],
    INT_CACHE_: [Object],
    TWO_PWR_16_DBL_: 65536,
    TWO_PWR_24_DBL_: 16777216,
    TWO_PWR_32_DBL_: 4294967296,
    TWO_PWR_31_DBL_: 2147483648,
    TWO_PWR_48_DBL_: 281474976710656,
    TWO_PWR_64_DBL_: 18446744073709552000,
    TWO_PWR_63_DBL_: 9223372036854776000,
    ZERO: [Object],
    ONE: [Object],
    NEG_ONE: [Object],
    MAX_VALUE: [Object],
    MIN_VALUE: [Object],
    TWO_PWR_24_: [Object],
    Long: [Circular] },
 MinKey: { [Function: MinKey] MinKey: [Circular] },
 MaxKey: { [Function: MaxKey] MaxKey: [Circular] },
 ObjectID: 
  { [Function: ObjectID]
    index: 2488094,
    createPk: [Function: createPk],
    createFromTime: [Function: createFromTime],
    createFromHexString: [Function: createFromHexString],
    isValid: [Function: isValid],
    ObjectID: [Circular],
    ObjectId: [Circular] },
 ObjectId: 
  { [Function: ObjectID]
    index: 2488094,
    createPk: [Function: createPk],
    createFromTime: [Function: createFromTime],
    createFromHexString: [Function: createFromHexString],
    isValid: [Function: isValid],
    ObjectID: [Circular],
    ObjectId: [Circular] },
 Symbol: { [Function: Symbol] Symbol: [Circular] },
 Timestamp: 
  { [Function: Timestamp]
    fromInt: [Function],
    fromNumber: [Function],
    fromBits: [Function],
    fromString: [Function],
    INT_CACHE_: [Object],
    TWO_PWR_16_DBL_: 65536,
    TWO_PWR_24_DBL_: 16777216,
    TWO_PWR_32_DBL_: 4294967296,
    TWO_PWR_31_DBL_: 2147483648,
    TWO_PWR_48_DBL_: 281474976710656,
    TWO_PWR_64_DBL_: 18446744073709552000,
    TWO_PWR_63_DBL_: 9223372036854776000,
    ZERO: [Object],
    ONE: [Object],
    NEG_ONE: [Object],
    MAX_VALUE: [Object],
    MIN_VALUE: [Object],
    TWO_PWR_24_: [Object],
    Timestamp: [Circular] },
    connect: [Circular] },
curCol: 'fs' }
Creating writestream
POST /createpost 500 32.185 ms - 1853

With the following error in my browser:

TypeError: undefined is not a function
        at GridStore.collection (/Users/sebastianhernandez/Desktop/instagram-project/instagram-ads/insta-famous/node_modules/mongodb/lib/gridfs/grid_store.js:513:18)
        at GridStore.open (/Users/sebastianhernandez/Desktop/instagram-project/instagram-ads/insta-famous/node_modules/mongodb/lib/gridfs/grid_store.js:187:27)
        at GridWriteStream._open (/Users/sebastianhernandez/Desktop/instagram-project/instagram-ads/insta-famous/node_modules/gridfs-stream/lib/writestream.js:96:14)
        at new GridWriteStream (/Users/sebastianhernandez/Desktop/instagram-project/instagram-ads/insta-famous/node_modules/gridfs-stream/lib/writestream.js:72:7)
        at Grid.createWriteStream (/Users/sebastianhernandez/Desktop/instagram-project/instagram-ads/insta-famous/node_modules/gridfs-stream/lib/index.js:42:10)
        at /Users/sebastianhernandez/Desktop/instagram-project/instagram-ads/insta-famous/routes/index.js:315:25
        at Layer.handle [as handle_request] (/Users/sebastianhernandez/Desktop/instagram-project/instagram-ads/insta-famous/node_modules/express/lib/router/layer.js:82:5)
        at next (/Users/sebastianhernandez/Desktop/instagram-project/instagram-ads/insta-famous/node_modules/express/lib/router/route.js:110:13)
        at done (/Users/sebastianhernandez/Desktop/instagram-project/instagram-ads/insta-famous/node_modules/multer/lib/make-middleware.js:41:7)
        at indicateDone (/Users/sebastianhernandez/Desktop/instagram-project/instagram-ads/insta-famous/node_modules/multer/lib/make-middleware.js:45:51)

I realize this isn't stack overflow but I feel like I wrote my code as closely as possible to the examples provided within the realm of my project. Any ideas?

Reggino commented 9 years ago

My version of node_modules/mongodb/lib/gridfs/grid_store.js... looks a bit different. What version of mongodb do you have installed?

prcbass commented 9 years ago

I have version 3.0.3 installed. Is there a newer version I should be using? I see that there is a version 3.0.4 out now.

UPDATE: I installed the newest version of mongo in a copy of my project but I am still getting the same error as far as I can tell

TypeError: undefined is not a function
        at GridStore.collection (/Users/sebastianhernandez/Desktop/instagram-project-3/instagram-ads/insta-famous/node_modules/mongodb/lib/gridfs/grid_store.js:513:18)
        at GridStore.open (/Users/sebastianhernandez/Desktop/instagram-project-3/instagram-ads/insta-famous/node_modules/mongodb/lib/gridfs/grid_store.js:187:27)
    at GridWriteStream._open (/Users/sebastianhernandez/Desktop/instagram-project-3/instagram-ads/insta-famous/node_modules/gridfs-stream/lib/writestream.js:96:14)
        at new GridWriteStream (/Users/sebastianhernandez/Desktop/instagram-project-3/instagram-ads/insta-famous/node_modules/gridfs-stream/lib/writestream.js:72:7)
        at Grid.createWriteStream (/Users/sebastianhernandez/Desktop/instagram-project-3/instagram-ads/insta-famous/node_modules/gridfs-stream/lib/index.js:42:10)
        at /Users/sebastianhernandez/Desktop/instagram-project-3/instagram-ads/insta-famous/routes/index.js:315:25
        at Layer.handle [as handle_request] (/Users/sebastianhernandez/Desktop/instagram-project-3/instagram-ads/insta-famous/node_modules/express/lib/router/layer.js:82:5)
        at next (/Users/sebastianhernandez/Desktop/instagram-project-3/instagram-ads/insta-famous/node_modules/express/lib/router/route.js:110:13)
        at done (/Users/sebastianhernandez/Desktop/instagram-project-3/instagram-ads/insta-famous/node_modules/multer/lib/make-middleware.js:41:7)
        at indicateDone (/Users/sebastianhernandez/Desktop/instagram-project-3/instagram-ads/insta-famous/node_modules/multer/lib/make-middleware.js:45:51)

Strangely enough, when I run db.version() in my new installation of mongodb I still get 3.0.3. Maybe I didn't do the installation correctly? After unpacking the mongodb .tar I ran mongod --dbpath /path/to/data/folder using the existing data folder in my project.

Reggino commented 9 years ago

I'm not familiar with the monk package you are using, but i guess the db-handle you get from it, is not exactly the same as a mongodb db-handle.

Could you try passing in a mongo.Db instance in the Grid-constructor as seen here?

If this does not solve your issue, please isolate your code further and add it as a test to the project so i can look into it further... thnx

ShreyasDol commented 6 years ago

same error type

db.db is not a function

TypeError: db.db is not a function at D:\A imp software file\MEAN developer\expressjs\expresscurd\routes\account.js:24:28 at Layer.handle [as handle_request] (D:\A imp software file\MEAN developer\expressjs\expresscurd\node_modules\express\lib\router\layer.js:95:5) at next (D:\A imp software file\MEAN developer\expressjs\expresscurd\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (D:\A imp software file\MEAN developer\expressjs\expresscurd\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (D:\A imp software file\MEAN developer\expressjs\expresscurd\node_modules\express\lib\router\layer.js:95:5) at D:\A imp software file\MEAN developer\expressjs\expresscurd\node_modules\express\lib\router\index.js:281:22 at Function.process_params (D:\A imp software file\MEAN developer\expressjs\expresscurd\node_modules\express\lib\router\index.js:335:12) at next (D:\A imp software file\MEAN developer\expressjs\expresscurd\node_modules\express\lib\router\index.js:275:10) at Function.handle (D:\A imp software file\MEAN developer\expressjs\expresscurd\node_modules\express\lib\router\index.js:174:3) at router (D:\A imp software file\MEAN developer\expressjs\expresscurd\node_modules\express\lib\router\index.js:47:12)

code var express = require('express'); var router = express.Router();

router.get('/', function (req, res, next) { var db = req.db; var accountTable = db.get('account'); accountTable.find({}, {}, function(errors, accounts) { var data = { accounts : accounts }; res.render('account/index', data); }); });

router.get('/add', function (req, res, next) { res.render('account/add'); });

router.post('/add', function (req, res, next) { var db = req.db; var accountTable = db.db('account'); var account ={ username: req.body.username, password: req.body.password, fullname: req.body.fullname }; accountTable.insert(account, function(errors, result){ res.redirect('/account/add'); }); });

module.exports = router;