Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mongoose and gridfs-locking-stream interaction #131

Open
ghasemikasra39 opened this issue Mar 21, 2018 · 9 comments
Open

mongoose and gridfs-locking-stream interaction #131

ghasemikasra39 opened this issue Mar 21, 2018 · 9 comments

Comments

@ghasemikasra39
Copy link

Sorry if the title is addressing gridfs-locking-stream and not gridfs-stream. I was using gridfs-locking-stream and had this problem:
https://stackoverflow.com/questions/49325749/mongoose-and-gridfs-locking-stream-interaction
I think gridfs-stream is not compatible with mongoose new version.
I've also created this issue on gridfs-locking-stream github page:
vsivsi/gridfs-locking-stream#5

@arn-the-long-beard
Copy link

Hello.

I also get the same error

Error: missing db argument new Grid(db, mongo)

Then if I replace with
``javascript
const conn = mongoose.connection

var Grid = require('gridfs-stream');
Grid.mongo = mongoose.mongo
const gfs = Grid(conn)
``

Then I get

"TypeError: Cannot read property 'readPreference' of null

I am using :

  • "mongoose": "^5.0.9",
  • MongoDB Version | 3.6.2
  • "gridfs-stream": "^1.1.1"

Best regards,

Arn

@deresegetachew
Copy link

my experience is i was using gridfs-stream with multer-gridfs-storage
for some reason mongoose:"^5.0" has issues with gridfs-stream when making connections and when i managed to make the connections multer-gridfs-stroage failed. so whatI resorted to was to use two mongoose versions in my application. At the end of the day i had different connections one to my GridFS and another to my mongoDatabase ; therefor i went on to use V5.0 for my mongodb and v4. for my GridFS. on how to install same packages of different version follow this https://gist.github.com/mnpenner/c46a5eff34cfa91f361a8b3baa249a10

@ldevalbray
Copy link

Hey I have the same issue ...
Any solution found ? I would like to avoid @deresegetachew 's one ;)

@stelapo
Copy link

stelapo commented Dec 13, 2018

Hey I have the same issue ...
Any solution found ?

@ghost
Copy link

ghost commented Jan 23, 2019

Any solutions ?

@zenbert5
Copy link

zenbert5 commented Jan 24, 2019

for reference i have encountered this. two difference instances of this happened with my code --

  1. mongo server isn't accessible for whatever reason

  2. modified my connection code -->
    const conn = mongoose.createConnection('your URI');
    var gfs;
    conn.once('open', () => {
    gfs = Grid(conn.db, mongoose.mongo);
    })

    from const conn = mongoose.connection(...)

    "express": "^4.16.4",
    "gridfs-stream": "^1.1.1",
    "mongoose": "^5.4.5",

mongo DB v4.02

Good luck

@kuzdogan
Copy link

kuzdogan commented Oct 3, 2019

Refer to here: #56

Using the db object instead of the connection resolved my issue.
So instead of this:

var gfs;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  console.log("Connected!")
  var mongoDriver = mongoose.mongo;
  gfs = new Gridfs(db, mongoDriver);
});

To this

var gfs;
var connection = mongoose.connection;
connection.on('error', console.error.bind(console, 'connection error:'));
connection.once('open', function() {
  console.log("Connected!")
  var mongoDriver = mongoose.mongo;
  gfs = new Gridfs(connection.db, mongoDriver);
});

@MZeeshan373
Copy link

MZeeshan373 commented Feb 23, 2021

Refer to here: #56

Using the db object instead of the connection resolved my issue.
So instead of this:

var gfs;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  console.log("Connected!")
  var mongoDriver = mongoose.mongo;
  gfs = new Gridfs(db, mongoDriver);
});

To this

var gfs;
var connection = mongoose.connection;
connection.on('error', console.error.bind(console, 'connection error:'));
connection.once('open', function() {
  console.log("Connected!")
  var mongoDriver = mongoose.mongo;
  gfs = new Gridfs(connection.db, mongoDriver);
});

Thanks @kuzdogan This resolved my issue .

@Chris-QIU
Copy link

Chris-QIU commented Jun 19, 2021

Refer to here: #56

Using the db object instead of the connection resolved my issue.
So instead of this:

var gfs;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  console.log("Connected!")
  var mongoDriver = mongoose.mongo;
  gfs = new Gridfs(db, mongoDriver);
});

To this

var gfs;
var connection = mongoose.connection;
connection.on('error', console.error.bind(console, 'connection error:'));
connection.once('open', function()  {
  console.log("Connected!")
  var mongoDriver = mongoose.mongo;
  gfs = new Gridfs(connection.db, mongoDriver);
});

@kuzdogan Thank you very much, this works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants