当我尝试在 MongoDB 数据库中上传图像时,出现 multer 错误

Posted

技术标签:

【中文标题】当我尝试在 MongoDB 数据库中上传图像时,出现 multer 错误【英文标题】:When i trying to upload images in MongoDB database, I'm getting a multer error 【发布时间】:2021-05-03 04:04:00 【问题描述】:

我使用 Gridfs Storage 存储图像并使用 multer 将图像上传到 MongoDB atlas 数据库中。 我的数据库已连接,我尝试使用 Postman 上传图像。但这在 multer 中显示了一个错误,在控制台中显示了意外的字段错误。

这里是 server.js

import express from 'express'
import mongoose from 'mongoose'
import cors from 'cors'
import multer from 'multer'
import GridsFsStorage from 'multer-gridfs-storage'
import Grid from 'gridfs-stream'
import bodyParser from 'body-parser'
import path from 'path'
import Pusher from 'pusher'


Grid.mongo=mongoose.mongo

//app config

const app=express()
const port=process.env.PORT || 8000

//middlewares

app.use(bodyParser.json())
app.use(cors())

//db config

const mongoURI='mongodb+srv://avishakcb:CiKas0n86a1A7Xsm@project350v1.kmy5r.mongodb.net/feeddb?retryWrites=true&w=majority'

const conn=mongoose.createConnection(mongoURI,
    useCreateIndex: true,
    useNewUrlParser:true,
    useUnifiedTopology:true
);

mongoose.connect(mongoURI,
    useCreateIndex:true,
    useNewUrlParser:true,
    useUnifiedTopology:true
)

mongoose.connection.once('open',()=>
    console.log('DB connected')
)

let gfs

conn.once('open',()=>
    console.log('DB Connected')
    gfs=Grid(conn.db,mongoose.mongo)
    gfs.collection('images')
)

const storage=new GridsFsStorage(
    url:mongoURI,
    file:(req,file)=>
        return new Promise((resolve,reject)=>
            const filename=`image-$Date.now()$path.extname(file.originalname)`

            const fileInfo=
                filename:filename,
                bucketName:'images'
            ;

            resolve(fileInfo);
        );
    
);

const upload=multer(storage);



//api routes
app.get('/',(req,res)=>res.status(200).send('hello'))

app.post('/upload/image',upload.single('file'),(req,res)=>
    res.status(201).send(req.file)
)


//listen 

app.listen(port,()=>console.log(`port $port is running`))

我在控制台日志中遇到的错误。

MulterError: Unexpected field
    at wrappedFileFilter (E:\Complete_web_development\TechBook\Tech_community\backend\node_modules\multer\index.js:40:19)
    at Busboy.<anonymous> (E:\Complete_web_development\TechBook\Tech_community\backend\node_modules\multer\lib\make-middleware.js:114:7)
    at Busboy.emit (events.js:315:20)
    at Busboy.emit (E:\Complete_web_development\TechBook\Tech_community\backend\node_modules\busboy\lib\main.js:38:33)
    at PartStream.<anonymous> (E:\Complete_web_development\TechBook\Tech_community\backend\node_modules\busboy\lib\types\multipart.js:213:13)
    at PartStream.emit (events.js:315:20)
    at HeaderParser.<anonymous> (E:\Complete_web_development\TechBook\Tech_community\backend\node_modules\dicer\lib\Dicer.js:51:16)   
    at HeaderParser.emit (events.js:315:20)
    at HeaderParser._finish (E:\Complete_web_development\TechBook\Tech_community\backend\node_modules\dicer\lib\HeaderParser.js:68:8) 
    at SBMH.<anonymous> (E:\Complete_web_development\TechBook\Tech_community\backend\node_modules\dicer\lib\HeaderParser.js:40:12)    
    at SBMH.emit (events.js:315:20)
    at SBMH._sbmh_feed (E:\Complete_web_development\TechBook\Tech_community\backend\node_modules\streamsearch\lib\sbmh.js:159:14)     
    at SBMH.push (E:\Complete_web_development\TechBook\Tech_community\backend\node_modules\streamsearch\lib\sbmh.js:56:14)
    at HeaderParser.push (E:\Complete_web_development\TechBook\Tech_community\backend\node_modules\dicer\lib\HeaderParser.js:46:19)   
    at Dicer._oninfo (E:\Complete_web_development\TechBook\Tech_community\backend\node_modules\dicer\lib\Dicer.js:197:25)
    at SBMH.<anonymous> (E:\Complete_web_development\TechBook\Tech_community\backend\node_modules\dicer\lib\Dicer.js:127:10)

【问题讨论】:

【参考方案1】:

文件功能收到承诺了吗?

file: (req, file) => 
  const filename = `image-$Date.now()$path.extname(file.originalname)`

  return 
    filename: filename,
    bucketName: 'images'
  ;

尝试直接退货

【讨论】:

不,这不起作用。它在 WrappedFileFilter 给出错误 根据 multer doc,LIMIT_UNEXPECTED_FILE 表示您的字段名称不在请求正文中

以上是关于当我尝试在 MongoDB 数据库中上传图像时,出现 multer 错误的主要内容,如果未能解决你的问题,请参考以下文章

使用 Multer 将图像上传到 mongoDB 时 Heroku 出错

MongoDB & Multer - 如何将缓冲区二进制文件转换为图像?

Firebase Storage 在图像上传时抛出错误(Nodejs)

如何将图像上传到 Cloudinary - MERN 堆栈

当我尝试上传带口罩的图像时,认知面部检测不起作用

当我尝试将图像上传到 Firebase 存储时,putFile 在 kotlin 中不起作用