Axios帖子未处理响应

时间:2019-06-18 06:55:03

标签: node.js reactjs express axios

我正在将图像上传到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)})
    })

再次,我只希望能够查看并保存响应,但是之后的所有内容都将无法执行。

1 个答案:

答案 0 :(得分:0)

您需要在服务器代码中删除括号

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

或  然后在服务器代码中添加return

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