cannot access before initialization

Viewed 24

I'm new in Node.js and OOP and need some help with my upload images system based on GridFS. In my class I have "initializeStorage" method and in the last line of that method "this.upload" works as middleware with I need to use in my routes file. In routes file I tried use "new" keyword to my class but then I was getting undiefiend error, I tried few others ways to solve that problem but I'm run of ideas. If someone have solution or hint lemme know and write below. Thanks.

GridFS class file

class MyGridFS {
  constructor() {
    let gfs = this.gfs;
    let gridfsBucket = this.gridfsBucket;
  }
  initializeGridFS(conn) {
    this.conn = conn;
    conn.once("open", () => {
      this.gridfsBucket = new mongoose.mongo.GridFSBucket(conn.db, {
        bucketName: "uploads",
      });
      this.gfs = Grid(conn.db, mongoose.mongo);
      this.gfs.collection("uploads");
    });
  }
  initializeStorage(mongo_uri) {
    this.mongo_uri = mongo_uri;
    const storage = new GridFsStorage({
      url: this.mongo_uri,
      file: (req, file) => {
        const filename = "file_" + Date.now() + path.extname(file.originalname);
        return {
          filename,
          bucketName: "uploads",
        };
      },
    });
    this.upload = multer({ storage });
  }
Routes file

upload.post("/upload", uploadMiddleware.single("file"), (req, res) =>
  uploadController.upload(req, res),
);

In "uploadMiddleware" place should be upload middleware from class before
0 Answers
Related