我是一个MongoDB和GridFS的初学者。我有一段代码,它将传入的图像保存到数据库中。但是,在保存之前我想调整图像大小。我尝试使用Sharp进行调整,但我不太理解它的工作原理。
我尝试了以下代码,但它没有起作用
const storage = new GridFsStorage({
url: mongoURI,
file: (req, file) => {
return new Promise((resolve, reject) => {
const filename = req.params.userID;
const fileInfo = {
filename: filename,
bucketName: "ProfilePic",
};
resolve(fileInfo);
});
}
});
const upload = multer({ storage });
router.post("/setProfilePic/:userID", upload.single("picture"), async(req, res) => {
//code not working
await sharp(req.file.path)
.resize(500)
.jpeg({quality: 50})
.toFile(
path.resolve(req.file.destination,'resized',image)
)
// end of non working code
return res.status(200).json({state: "ok"})
});
try..catch
块包围await
并记录错误? - Kees de Kooter