aheckmann / gridfs-stream

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

TypeError: Cannot read property 'primary' of undefined #20

Closed lamalex closed 11 years ago

lamalex commented 11 years ago

/Users/alexlauni/ptlndiy-dev/node_modules/mongodb/lib/mongodb/gridfs/gridstore.js:132 if((self.mode == "w" || self.mode == "w+") && self.db.serverConfig.primary ! ^ TypeError: Cannot read property 'primary' of undefined at Stream.GridStore.open (/Users/alexlauni/ptlndiy-dev/node_modules/mongodb/lib/mongodb/gridfs/gridstore.js:132:69) at Stream._open (/Users/alexlauni/ptlndiy-dev/node_modules/gridfs-stream/lib/writestream.js:166:15) at Stream.write (/Users/alexlauni/ptlndiy-dev/node_modules/gridfs-stream/lib/writestream.js:87:10) at write (_stream_readable.js:557:24) at flow (_stream_readable.js:566:7) at ReadStream.pipeOnReadable (_streamreadable.js:598:5) at ReadStream.EventEmitter.emit (events.js:92:17) at emitReadable (_stream_readable.js:392:10) at emitReadable (_stream_readable.js:388:5) at readableAddChunk (_stream_readable.js:150:9)

var fs = require('fs'); var Grid = require('gridfs-stream'); var mongoose = require('mongoose');

exports.newMemory = function(req, res) { var writestream = gfs.createWriteStream({filename:'goldar.png'}); fs.createReadStream(req.files.pic.path).pipe(writestream); writestream.on('close', function(file) { console.log('wrote file'); }); };

thank you!

aheckmann commented 11 years ago

what version of the driver?

please post code we can use to reproduce the issue. its not clear if this is a bug in the driver or this project.

lamalex commented 11 years ago

i think it was my fault, i rewrote my code and now it's working like a charm! sorry for not closing this issue!

aheckmann commented 11 years ago

k thanks!

voz commented 11 years ago

I have the same error using similar code, what could be the reason?

The error:

node_modules/mongoose/node_modules/mongodb/lib/mongodb/gridfs/gridstore.js:145
<   if((self.mode == "w" || self.mode == "w+") && self.db.serverConfig.primary !
<                                                              
<        ^
< TypeError: Cannot read property 'primary' of undefined
<     at Stream.GridStore.open (/Users/boos/code/graspio/node_modules/mongoose/node_modules/mongodb/lib/mongodb/gridfs/gridstore.js:145:69)
<     at Stream._open (/Users/boos/code/graspio/node_modules/gridfs-stream/lib/writestream.js:166:15)
<     at Stream.write (/Users/boos/code/graspio/node_modules/gridfs-stream/lib/writestream.js:87:10)
<     at write (_stream_readable.js:583:24)
<     at flow (_stream_readable.js:592:7)
<     at ReadStream.pipeOnReadable (_stream_readable.js:624:5)
<     at ReadStream.EventEmitter.emit (events.js:92:17)
<     at emitReadable_ (_stream_readable.js:408:10)
<     at emitReadable (_stream_readable.js:404:5)
<     at readableAddChunk (_stream_readable.js:165:9)
program terminated

The code:

exports.upload = function(req, res){
  var tempfile    = req.files.file.path;
  var origname    = req.files.file.name;
  var writestream = app.gfs.createWriteStream({ filename: origname });

  // open a stream to the temporary file created by Express
  fs.createReadStream(tempfile)
    .on('end', function() {
      res.send('OK');
    })
    .on('error', function() {
      res.send('ERR');
    })
    .pipe(writestream);
}

I'm using the following versions:

db version v2.4.6
Wed Oct  2 12:15:28.558 git version: nogitversion
mongoose@3.6.20 

What additional info should I provide? Thanks.

voz commented 11 years ago

One more addition, I'm using mongoose to construct a grid fs:

mongoose = require("mongoose");
gridfs = require('gridfs-stream');
gridfs.mongo = mongoose.mongo;

db = mongoose.connect(config.db);

conn = db.connection;
conn.once("open", callback = function() {
  app.logger.info("MongoDB connection established");
  return app.gfs = gridfs(db);
});
voz commented 11 years ago

Sorry, it's my mistake. I should have used var conn = mongoose.createConnection(..); to create the connection, as it's described in the tutorial. Now everything works well.

leighghunt commented 10 years ago

@voz thanks for your comments above - it helped me pinpoint the error in my code - I was passing the connection object from mongoose.createConnection(...) rather than connection.db, owing to my own confusing naming scheme!