0

I'm setting up a server to run my project and using multer on the backend. However, whenever I try to send an image from my frontend to the backend, i get message: "Unexpected field", name: "MulterError", frames.

dependencies that i'm using in my server.js:

const express = require('express')
const multer  = require('multer')
const upload = multer({ dest: 'uploads/' })
const app = express()
var singleUpload = upload.single('featuredImage');
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const cors = require('cors');
var axios = require("axios");

code in server.js:

 app.use(bodyParser.json())
 app.use(cookieParser())

 app.post('/uploads', singleUpload, function (req, res, next) {
     axios.post('http://api.com/api/public/nuxt', 
      upload,
     {     
      headers: 
     {
     'Content-Type': 'multipart/form-data',
     }   
    })
 .then(function(result) {
    console.log('SUCCESS!!!!!!!!!!!');
    console.log(result.data);

 }) .catch(function(){ 
   console.log('FAILURE!!!!1!!!!');
 });
   return res.json({"status":"success"})
})

 module.exports = app
marjan
  • 97
  • 7
  • The field name of the image you want to upload in form-data must be same as the name defined in the `upload.single()` function (`featuredImage` in your code). What is the name you currently use for it? – Duy Quoc Aug 22 '22 at 08:10
  • @DuyQuoc problem fixed thanks. but i have another problem that content-length is 227 and file size is 14980. it means somehow that hasn't recieved the file. do you have any opinion? – marjan Aug 23 '22 at 04:26
  • I don't know what your purpose is when you use `axios.post('http://api.com/api/public/nuxt'...`. Your file upload has been handled in the middleware `singleUpload` already. – Duy Quoc Aug 23 '22 at 06:58

0 Answers0