Actually, I want to use gfs
variable outside of the function. But I'm getting undefined
. I'm using Node.js for the back-end and using Express.js framework.
Please take a quick look at what I'm doing.
const mongoose = require("mongoose"); // Version = 6.2.10
const Grid = require("gridfs-stream"); // Version = 1.1.1
// Creating the mongodb connection
const mongoURI = "mongodb://localhost:27017/test";
const conn = mongoose.createConnection(mongoURI);
// Init gfs
let gfs;
conn.once("open", async () => {
gfs = Grid(conn.db, mongoose.mongo);
gfs.collection("uploads");
console.log(gfs); // Returns the Grid object
});
console.log(gfs); // Returns "undefined" ===> Need to solve the bug here.
I faced similar problem what worked for me is