expressjs / expressjs/multer

MulterError: Unexpected field when trying to use different storage configuration

Open
#1,239 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
12.1k
Forks
1.1k
Avg merge
8d 2h
Merged PRs (30d)
21

Description

`const express = require('express')
const mongoose = require('mongoose')
const jwt = require('jsonwebtoken')
const fetchadmin = require('../../middleware/fetchadmin');
const router = express.Router();
const admin = require("firebase-admin");
const multer = require('multer');
const serviceAccount = require("./serviceAccountKey.json");
const Admin = mongoose.model('Admin');
const Doctor = mongoose.model('Doctor');
const cloudinary = require('../../helper/imageUpload')

// Profile Photo Storage

const imageStorage = multer.diskStorage({});

const fileFilter = (req, file, cb) => {
console.log(file)
if (file.mimetype.startsWith('image')) {
cb(null, true);
} else {
cb('invalid image file!', false);
}
};
const uploads = multer({ storage: imageStorage, fileFilter });

//File Storage

admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
storageBucket: "medease-9368b.appspot.com",
});

const bucket = admin.storage().bucket();

const storage = multer.memoryStorage();
const upload = multer({ storage: storage });

router.post('/adddoctor', fetchadmin, uploads.single('profile'), upload.single("file"), async (req, res) => {

const file = req.file;

if (!req.user)
return res
.status(401)
.json({ success: false, message: 'unauthorized access!' });

if (!file) {
return res.status(400).send("No file uploaded.");
}

//File Upload

const fileRef = bucket.file(file.originalname);
const blobStream = fileRef.createWriteStream();

blobStream.on("error", (error) => {
res.status(500).send("File upload error: " + error);
});

blobStream.on("finish", () => {
fileRef.makePublic().then(async () => {
const med_license = `https://storage.googleapis.com/${bucket.name}/${fileRef.name}`;

try {

const jsonData = JSON.parse(req.body.data);
const { name, gender, dob, email, phone, address, licene_number, specialization, experience, qualification, med_school, graduation_year, gov_id, username, password } = jsonData;

const existingUser = await Doctor.findOne({ username });
if (existingUser) {
return res.status(409).send({ message: 'Username Already Taken' });
}

// Cloudinary upload code
const result = await cloudinary.uploader.upload(req.file.path, {
public_id: `${req.user._id}_profile`,
width: 500,
height: 500,
crop: 'fill',
});

const Avatar = result.url;

const newDoctor = new Doctor({ name, gender, dob, email, phone, address, licene_number, specialization, experience, qualification, med_school, graduation_year, gov_id, med_license, username, password, Avatar });
await newDoctor.save();

res.status(201).json({ message: 'Doctor registered successfully' });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal server error' });
}

})

});

blobStream.end(file.buffer);
});

module.exports = router`

![File issue](https://github.com/expressjs/multer/assets/95435900/f1609130-51ad-41bb-a724-bc2666ad119f)
![Error](https://github.com/expressjs/multer/assets/95435900/d534d269-0807-4520-9529-865bd74e71d1)

I am trying to send two documents using postman to the backend and store one file on firebase and other on cloudinary using different configurations but i am encountering this error. If anyone has solution to this problem then Please!! do provide.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.