Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to replace gridStore to gridFSBucket?

I have this error message:

(node:11976) DeprecationWarning: GridStore is deprecated, and will be removed in a future version. Please use GridFSBucket instead

and sometimes I have trouble viewing the picture , I guess because of that, due to poor documentation I have no idea how to switch my code to GridFSBucket, this is it:

conn.once("open", () => {
  // Init stream
  gfs = Grid(conn.db, mongoose.mongo);
  //gfs = new mongoose.mongo.GridFSBucket(conn.db);
  gfs.collection("user_images");
});


var storageImage = new GridFsStorage({
  url: dbURI,
  options: { useNewUrlParser: true, useUnifiedTopology: true },
  file: (req, file) => {
    return new Promise((resolve, reject) => {
      crypto.randomBytes(16, (err, buf) => {
        if (err) {
          return reject(err);
        }
        const filename = buf.toString("hex") + path.extname(file.originalname);
        const fileInfo = {
          filename: filename,
          bucketName: "user_images"
        };
        resolve(fileInfo);
      });
    });
  }
});
const uploadImage = multer({ storage: storageImage });

    const uploadImage = multer({ storage: storageImage });
router.get("/image/:filename", (req, res) => {
  gfs.files.findOne({ filename: req.params.filename }, (err, file) => {
    if (!file || file.length === 0) {
      return res.status(404).json({
        err: "No file exists"
      });
    }

    if (file.contentType === "image/jpeg" || file.contentType === "image/png") {
      const readstream = gfs.createReadStream(file.filename);
      //const readstream = gridFSBucket.openUploadStream(file.filename);
      readstream.pipe(res);
    } else {
      res.status(404).json({
        err: "Not an image"
      });
    }
  });
});

I would really appreciate the help, what do I need to change here to make it work with GridFsBucket, huge thanks in advance!

like image 496
Alexander Avatar asked Jan 13 '20 12:01

Alexander


People also ask

Is GridFS deprecated?

the solution you are proposing uses the deprecated GridFS package that throws a deprecation warning if you don't update to the GridFSBucket package.

What is GridFSBucket?

A GridFS “bucket” is the combination of an “fs. files” and “fs. chunks” collection which together represent a bucket where GridFS files can be stored.

What is GridFSBucket in Mongodb?

GridFSBucket(db); Pass your bucket name as the second parameter to the create() method to create or reference a bucket with a custom name other than the default name fs , as shown in the following example: const bucket = new mongodb.


1 Answers

I ended up having the same issue, you have most likely determined that readstream = gfs.createReadStream(file.filename); is what is causing the error. Just need to add a new variable and change one line.

//add var
let gridFSBucket;
let gfs;
connection.once('open', () => {
  gfs = Grid(conn.db, mongoose.mongo);
  // add value to new var
  gridFSBucket = new mongoose.mongo.GridFSBucket(conn.db, {
    bucketName: 'user_images'
  });

  gfs = Grid(connection.db, mongoose.mongo);
  gfs.collection(image_bucket_name);

  if (file.contentType === 'image/jpeg' || file.contentType === 'image/png') {
    //now instead of const readstream = gfs.createReadStream(file.filename);
    //add this line
    const readStream = gridFSBucket.openDownloadStream(file._id);
    readSteam.pipe(res);
  }
});

If you run into ( DeprecationWarning: GridStore is deprecated, and will be removed in a future version. Please use GridFSBucket instead ), hope this saves you some time.

like image 119
General Butt Naked Avatar answered Sep 22 '22 07:09

General Butt Naked