我,我正在尝试使用 multer 和 multer gridfs 上传图像,但它给了我这个错误......请看看并建议我修复......提前谢谢。
Busboy 的 WrappedFileFilter(/home/atul/Documents/coding/project 和 practice/fb-clone-backend/node_modules/multer/index.js:40:19)中的 [MulterError: Unexpected field][1]。(/home/atul/Documents/coding/project and practice/fb-clone-backend/node_modules/multer/lib/make-middleware.js:114:7) 在 Busboy.emit (events.js:315:20) 在Busboy.emit (/home
//imports
import express from 'express';
import mongoose from 'mongoose';
import cors from 'cors';
import multer from 'multer';
import GridFsStorage from 'multer-gridfs-storage';
import Grid from 'gridfs-stream';
// import bodyParser from 'body-parser';
import path from 'path';
import Pusher from 'pusher';
// import { MongoClient } from 'mongodb';
Grid.mongo = mongoose.mongo;
//app config
const app = express();
const port = process.env.PORT || 8000;
//middlewares
app.use(express.json()); //Used to parse JSON bodies
app.use(express.urlencoded({ extended: true })); //Parse URL-encoded bodies
app.use(cors());
//db config
const mongoURI =
'mongodb+srv://admin:atul1210@cluster0.avlrm.mongodb.net/fbdb?retryWrites=true&w=majority';
const conn = mongoose.createConnection(mongoURI, {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
});
mongoose
.connect(mongoURI, {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log('connection successful...');
})
.catch((err) => {
console.log('connection failed...');
});
mongoose.connection.once('open', () => console.log('DB COnnected'));
var gfs;
conn.once('open', () => {
console.log('DB Connected');
gfs = Grid(conn.db, mongoose.mongo);
gfs.collection('images');
});
const storage = new GridFsStorage({
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: storage });
// console.log({ storage });
//api routes
app.get('/', (req, res) => res.status(200).send('hello world'));
app.post('/upload/image', upload.single('file'), (req, res) => {
console.log('>>>>>', req.file);
res.status(201).send(req.file);
});
// listen
app.listen(port, () => console.log(`listening on localhost ${port}`));
/atul/Documents/coding/project and practice/fb-clone-backend/node_modules/busboy/lib/main.js:38:33)在 PartStream。(/home/atul/Documents/coding/project and practice/fb-clone-backend/node_modules/busboy/lib/types/multipart.js:213:13) 在 PartStream.emit (events.js:315:20) 在标头解析器。(/home/atul/Documents/coding/project 和 practice/fb-clone-backend/node_modules/dicer/lib/Dicer.js:51:16) 在 HeaderParser.emit (events.js:315:20) 在 HeaderParser。 _finish (/home/atul/Documents/coding/project and practice/fb-clone-backend/node_modules/dicer/lib/HeaderParser.js:68:8) 在 SBMH。(/home/atul/Documents/coding/project and practice/fb-clone-backend/node_modules/dicer/lib/HeaderParser.js:40:12) 在 SBMH.emit (events.js:315:20) 在 SBMH。 _sbmh_feed (/home/atul/Documents/coding/project and practice/fb-clone-backend/node_modules/streamsearch/lib/sbmh.js:159: 14) 在 SBMH.push (/home/atul/Documents/coding/project and practice/fb-clone-backend/node_modules/streamsearch/lib/sbmh.js:56:14) 在 HeaderParser.push (/home/atul/ Documents/coding/project and practice/fb-clone-backend/node_modules/dicer/lib/HeaderParser.js:46:19) 在 Dicer._oninfo (/home/atul/Documents/coding/project and practice/fb-clone- backend/node_modules/dicer/lib/Dicer.js:197:25)在 SBMH。(/home/atul/Documents/coding/project and practice/fb-clone-backend/node_modules/dicer/lib/Dicer.js:127:10) _oninfo(/home/atul/Documents/coding/project and practice/fb-clone-backend/node_modules/dicer/lib/Dicer.js:197:25)在 SBMH。(/home/atul/Documents/coding/project and practice/fb-clone-backend/node_modules/dicer/lib/Dicer.js:127:10) _oninfo(/home/atul/Documents/coding/project and practice/fb-clone-backend/node_modules/dicer/lib/Dicer.js:197:25)在 SBMH。(/home/atul/Documents/coding/project and practice/fb-clone-backend/node_modules/dicer/lib/Dicer.js:127:10)