Сообщение Axios не обрабатывает ответ - PullRequest
0 голосов
/ 18 июня 2019

Я загружаю изображения в Cloudinary и хочу получить URL-адрес изображения после его загрузки. Он загружается правильно, и я могу console.log URL-адрес на стороне сервера, но после этого ответ не обрабатывается в вызове axios на стороне клиента, и сообщения об ошибках не появляются.

Мой клиент:

submitFile = () => {
    var formData = new FormData();
    formData.append('image', this.state.file, 'tmp_name');//this.state.file is not correct format compared to postman constructed
    axios.post('http://localhost:3000/upload', formData).then(function(response) {
      console.log(response);
    });
  }

Мой сервер:

  server.post('/upload', async (req, res, next) => {
    const upload = multer({ storage }).single('image')
    upload(req, res, function(err) {
      if (err) {
        return res.send(err)
      }
      const path = req.file.path
      cloudinary.uploader.upload(
        path,
        { public_id: `${filename()}` },
        async function(err, image) {
          if (err) return res.send(err)
          console.log('file uploaded to Cloudinary')
          // remove file from server
          const fs = require('fs')
          fs.unlinkSync(path)
        }
      ).then(result => {res.send(result)})
    })

Опять же, я хочу просто иметь возможность просмотреть ответ и сохранить его, но все после этого .then не выполняется.

1 Ответ

0 голосов
/ 18 июня 2019

Вам необходимо убрать скобку в тогда коде сервера

server.post('/upload', async (req, res, next) => {
    /* upload code*/
      ).then(result => res.send(result))
    })

ИЛИ Добавить возврат в тогда коде сервера

server.post('/upload', async (req, res, next) => {
    /*upload code*/
          ).then(result => { return res.send(result)})
        })
...