我正在尝试将User ID
传递给名为multer
的{{1}}函数,以便我可以上传以upload
为名称的图像。
User ID
是一个User ID
令牌,在使用前必须为JWT
。
我希望首先执行decoded
函数,以便我可以将authenticate
传递给User ID
函数。但是实际上发生的是先执行upload
,然后执行upload
,这意味着我无法在上传文件之前检查用户是否已通过身份验证。
authenticate
-
Upload
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, path)
},
filename: (req, file, cb) => {
// user ID fetched from the JWT cookie
console.log(req.user) // this should be the plaintext User ID if the authenticate function ran first.
req.myfiles = req.files // pass the file object to the next middleware
cb(null, `image.jpg`)
}
})
const fileFilter = (req, file, cb) => {
const mimeType = file.mimetype.toLowerCase()
console.log(file)
if (mimeType === 'image/jpeg' || mimeType === 'image/png') { // the uploaded file MUST be an image type
console.log('filter success')
cb(null, true)
}else {
console.log('file filter, failed')
cb(null, false) // reject if the uploaded file is not an image
}
}
-
authenticate