我收到了这个错误信息:
(node:11976) DeprecationWarning: GridStore is deprecated, and will be removed in a future version. Please use GridFSBucket instead
有时候我在查看图片的时候遇到了问题,我猜是因为文档不够详细,所以我不知道如何将代码切换到GridFSBucket,以下是我的代码:
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"
});
}
});
});
我非常感激您的帮助,请问我需要在这里做出哪些改变,以便与GridFsBucket一起使用?非常感谢!