Home > Back-end >  can't receive file with multer (node.js-nuxt3)
can't receive file with multer (node.js-nuxt3)

Time:06-04

I use nuxt3/node.js with multer , i can't store or receive file in my server and i can't see req.file in root, its empty all time. server Code:

const upload = multer({ dest: './uploads/' })
router.post('/sendNewQuestionCSV', upload.single('csv'),adminControler.sendNewQuestionCSV.bind(adminControler))

and my FrontEnd Code with nuxt3:

async function fileSelected(e) {
  let formData = new FormData();
  formData.append("csv", e.target.files[0]);
  const res = await $postFetch("/admin/sendNewQuestionCSV", formData, {
    "Content-Type": "multipart/form-data",
  });
}

note:$postFetch is an method i made my self use fetch and third argument is "headers". its a nuxt3 Plugin

this Plugin code:

export default defineNuxtPlugin(async () => {
  return {
    provide: {
      postFetch: async (url, body,headers={}) => {
        let token = useCookie('token');
        return await $fetch(url, {
          method: 'post',
          body: { token: token.value, ...body },
          baseURL: useRuntimeConfig().API_BASE,
          ...headers
        }).catch((error) => error.data)
      }
    }
  }
})

CodePudding user response:

try using .append to add the token:

  postFetch: async (url, body,headers={}) => {
    let token = useCookie('token');
    body.append('token', token.value);
    return await $fetch(url, {
      method: 'post',
      body: body,
      baseURL: useRuntimeConfig().API_BASE
    }).catch((error) => error.data)
  }

EDIT

also try removing headers

  • Related