изображение возвращено как undefined express пакет для загрузки файлов - PullRequest
0 голосов
/ 28 мая 2020

Я не уверен, что мой запрос на выборку может быть виноват, но я отправляю некоторые JSON, включая изображение, на свой сервер, однако при попытке зарегистрировать сторону файлового сервера я получаю undefined ?? Я использую загрузку файла express, настроенную в моем приложении. js файл

html выборка клиента

//define post parameters
async function postData(url = '',data={}) {

  // Default options are marked with *
  const response = await fetch(url, {
    method: 'POST', // *GET, POST, PUT, DELETE, etc.
    mode: 'no-cors', // no-cors, *cors, same-origin
    cache: 'no-cache', // *default , no-cache, reload, force-cache, only-if-cached
    credentials: 'same-origin', // include, *same-origin, omit
    headers: {
      //'Content-Type': 'application/json'
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    redirect: 'follow', // manual, *follow, error
    referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
    body: JSON.stringify(data) // body data type must match "Content-Type" header
  });
  return response.json(); // parses JSON response into native JavaScript objects
}


//MAKE POST
 document.getElementById("addpost").addEventListener('click',function(e){
      e.preventDefault()
      let tit = document.getElementById("title").value
      let bod = document.getElementById("body").value
      let img = document.getElementById("image").files[0]

      var formData = new FormData();
      formData.append('image', img)

      postData(`http://${window.location.hostname}:3000/admin/addpost`,{title: tit, body: bod, formData})
      .then((data)=>{console.log(data.data)})
    })

мой маршрут в express попытка для логирования файла

router.post('/admin/addpost',function (req,res,next) {

  const key = Object.keys(req.body)[0]
  const parsedKey = JSON.parse(key)
  const title = parsedKey.title
  const body = parsedKey.body

  console.log(req.body.image)

});
...