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!
the solution you are proposing uses the deprecated GridFS package that throws a deprecation warning if you don't update to the GridFSBucket package.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With