Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MulterError: Unexpected field when trying to use different storage configuration #1239

Open
ShaneCoelho opened this issue Jan 24, 2024 · 1 comment

Comments

@ShaneCoelho
Copy link

`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
Error

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.

@joeyguerra
Copy link

In the error message, I see fiieldname = profile, but that route handler is expecting a fileldname of file too. Are you trying to upload multiple files in 1 request? have you tried using upload.array?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants