bradtraversy/mongo_file_uploads

gfs not initializing

Closed this issue · 1 comments

Hi, I followed the code and was able to successfully upload files onto MongoDB with GridFsStorage. However, my GET functions to retrieve files run into errors: "can't read property 'files' of undefined"

I've tried debugging where gfs was initialized but I'm unsure where the problem is.

const conn = mongoose.createConnection(mongoURI);
// Init gfs
let gfs;
conn.once('open', () => {
// Init stream
console.log("Connected to DB");
gfs = Grid(conn.db, mongoose.mongo);
gfs.collection('uploads');
});

Somehow my gfs is undefined after initializing. I'm using mongoose 5.12.5, express 4.17.1, gridfs-stream 1.1.1, for reference.

gfs is initialized in the app.js but wouldn't work if is passed as a parameter into routes.