aheckmann / gridfs-stream

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

Getting error (Mongoose & GridFS) "TypeError: Cannot read property 'w' of undefined" #56

Closed mxrguspxrt closed 9 years ago

mxrguspxrt commented 9 years ago

Code:

mongoose.connect("mongodb://localhost/comics"); var db = mongoose.connection; db.once('open', function() { // .. Grid.current = Grid(db, mongoose.mongo); });

var gridWriteStream = Grid.current.createWriteStream({ _id: mongoose.Types.ObjectId(), mode: "w" });

Error:

node_modules/mongoose/node_modules/mongodb/lib/mongodb/gridfs/gridstore.js:1564 } else if(self.safe.w != null || typeof self.safe.j == 'boolean' || typeof s ^ TypeError: Cannot read property 'w' of undefined

Versions: "mongoose": "~3.8.20" "gridfs-stream": "0.5.3"

bigmeech commented 9 years ago

@mxrguspxrt this issue drove me insane some few months back.

the misleading part in the gridfs-stream documentation is this bit

"Options may contain zero or more of the following options",

Actually that is not entirely true as excluding the "w" attribute in the createWriteStream options object will throw the error you are experiencing. Adding it should fix that as it did for me.

follow the below link for full details from the original source http://mongodb.github.io/node-mongodb-native/api-generated/gridstore.html

mxrguspxrt commented 9 years ago

Hey!

Sorry, I think this can be closed. I rewrote. I was not using connection object, but db object, or some similar use case error.

I think there should be check that object is of correct type.