首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Axios post没有处理响应

Axios post没有处理响应
EN

Stack Overflow用户
提问于 2019-06-18 14:55:03
回答 1查看 72关注 0票数 0

我正在将图像上传到Cloudinary,并希望在上传后检索图像url。上传是正确的,我可以在服务器端console.log url,但之后响应不会在客户端的axios调用中处理,也没有出现错误消息。

我的客户:

代码语言:javascript
运行
复制
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);
    });
  }

我的服务器:

代码语言:javascript
运行
复制
  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都不会执行。

EN

回答 1

Stack Overflow用户

发布于 2019-06-18 15:30:05

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

代码语言:javascript
运行
复制
server.post('/upload', async (req, res, next) => {
    /* upload code*/
      ).then(result => res.send(result))
    })

或在服务器代码中添加返回

代码语言:javascript
运行
复制
server.post('/upload', async (req, res, next) => {
    /*upload code*/
          ).then(result => { return res.send(result)})
        })
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56643321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档