【问题标题】:Throwing and handling error from multer-gridfs-storage来自 multer-gridfs-storage 的抛出和处理错误
【发布时间】:2021-09-09 21:27:04
【问题描述】:

我从 multer-gridfs-storage 抛出一个错误,但不确定如何以更好的方式处理它。

我的代码:-

const storage = new gridfsStorage({
    url : 'mongodb://localhost:27017/uploadeditems' ,
    options : { useNewUrlParser : true , useUnifiedTopology : true  } ,
    file : (req , file) => {
        if(file.mimetype === 'image/jpeg') {
            return {
                filename : file.originalname,
                bucketName : 'Images' ,
                metadata : {
                    ownername : "Anshu Anand"
                }
            }
        }
        else if(file.mimetype === 'application/pdf') {
            return {
                filename : file.originalname , 
                bucketName : 'projectPDFs'
            }
        }

        else {
            throw new Error("File type is different")
        }
    }
})

upload = multer({
    storage ,
    
})

我的 api 来处理它:-

app.post('/upload' , upload.single('project')  , async (req, res) => {
    console.log(req.file)
    

    res.render('upload' , {
            msg : "File has been uploaded successfully"
        })
} ,

(err , req , res , next ) => {

    console.log(err) 

  return res.send(err) //gives just this :- storageError : [] 
  
)

console.log(err) 输出:-

Error: File type is different
at GridFSStorage.file [as _file] (F:\Node Projects\upload-download\index.js:41:19)
at GridFSStorage._generate (F:\Node Projects\upload-download\node_modules\multer-gridfs-storage\lib\gridfs.js:472:18)
at GridFSStorage.fromStream (F:\Node Projects\upload-download\node_modules\multer-gridfs-storage\lib\gridfs.js:368:35)
at GridFSStorage.fromFile (F:\Node Projects\upload-download\node_modules\multer-gridfs-storage\lib\gridfs.js:353:15)
at GridFSStorage._handleFile (F:\Node Projects\upload-download\node_modules\multer-gridfs-storage\lib\gridfs.js:314:9)
at F:\Node Projects\upload-download\node_modules\multer\lib\make-middleware.js:144:17
at allowAll (F:\Node Projects\upload-download\node_modules\multer\index.js:8:3)
at wrappedFileFilter (F:\Node Projects\upload-download\node_modules\multer\index.js:44:7)
at Busboy.<anonymous> (F:\Node Projects\upload-download\node_modules\multer\lib\make-middleware.js:114:7)
at Busboy.emit (node:events:376:20)
at Busboy.emit (F:\Node Projects\upload-download\node_modules\busboy\lib\main.js:38:33)
at PartStream.<anonymous> (F:\Node Projects\upload-download\node_modules\busboy\lib\types\multipart.js:213:13)
at PartStream.emit (node:events:376:20)
at HeaderParser.<anonymous> (F:\Node Projects\upload-download\node_modules\dicer\lib\Dicer.js:51:16)
at HeaderParser.emit (node:events:376:20)
at HeaderParser._finish (F:\Node Projects\upload-download\node_modules\dicer\lib\HeaderParser.js:68:8) {
 storageErrors: []
 }

那么我该如何以更好的方式处理它并发送我在存储配置中抛出的确切错误?

提前感谢您的帮助。

【问题讨论】:

    标签: javascript node.js upload multer multer-gridfs-storage


    【解决方案1】:

    我发现了如何获得准确的信息。 您可以在 err 的 message 属性中获取它。

    console.log(err.message) //=>File type is different
    

    【讨论】:

      猜你喜欢
      • 2018-09-26
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      • 2021-11-28
      • 2017-10-25
      • 1970-01-01
      • 2018-01-12
      • 1970-01-01
      相关资源
      最近更新 更多