实际上,我想在函数外使用gfs
变量。但是我得到的是undefined
。我在后端使用Node.js,并使用Express.js框架。请快速了解一下我在做什么。
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.
1条答案
按热度按时间mlnl4t2r1#
我也遇到过类似的问题,但对我有效的是