节点Js-无法获得响应

时间:2019-08-14 13:05:08

标签: javascript node.js reactjs file-upload

我正在尝试upload image,它已在S3中正确上传,数据库条目也运行良好,然后输入正确的else condition,但未发送任何响应。

它进入if (req.body.category === 'profile') {并到达else condition

一切正常,但是在浏览器中测试时我没有收到响应。

在crome控制台中,我看到状态=> failed

但是它在POSTMAN

中工作正常
router.post('/attachment', async (req, res) => {
  const singleUpload = await fileUpload.single('fileUpload')
  singleUpload(req, res, async (err) => {
    console.log('came to single upload')
    if (err) {
      console.log(err)
      let error = {}
      error.isError = true
      error.message = err.message
      res.status(500).json(error)
    } else {
      console.log('stored to s3')
      console.log(req.body.id)
      console.log(req.body.category)
      console.log(req.file)

        if (req.body.category === 'profile') {
          let result = await helper.setUserPhoto(req.body.id, req.file.location)
          console.log(`result: ${result}`)
          if (result === null) {
            let error = {}
            error.isError = true
            error.message = 'Unable to store url of user,try after some time'
            res.status(500).json(error)
          } else {
            let output = {
              originalname: req.file.originalname,
              size: req.file.size,
              location: req.file.location
            }
            console.log(output)
            //IT REACHES HERE, EVEN OUTPUT CONSOLE WORKS FINE
            res.status(200).json(output)
          }
        } else if (req.body.category === 'logo') {
          let result = await helper.setUserCompanyLogo(req.body.id, req.file.location)
          if (result === null) {
            let error = {}
            error.isError = true
            error.message = 'Unable to store url of user,try after some time'
            res.status(500).json(error)
          } else {
            let output = {
              originalname: req.file.originalname,
              size: req.file.size,
              location: req.file.location
            }
            res.status(200).json(output)
          }
        } else {
          res.status(400).json({
            isError: true,
            message: 'category is not valid'
          })
        }

    }
  })
})

刚才我尝试了https://netlog-viewer.appspot.com/#events

141948: URL_REQUEST
http://localhost:3000/api/user/attachment
Start Time: 2019-08-14 19:01:29.114

t=40011 [st=  0] +REQUEST_ALIVE  [dt=484]
                  --> priority = "MEDIUM"
                  --> traffic_annotation = 101845102
                  --> url = "http://localhost:3000/api/user/attachment"
t=40012 [st=  1]    DELEGATE_INFO  [dt=32]
                    --> delegate_blocked_by = "Opening Files"
t=40044 [st= 33]    NETWORK_DELEGATE_BEFORE_URL_REQUEST  [dt=0]
t=40044 [st= 33]   +URL_REQUEST_START_JOB  [dt=450]
                    --> load_flags = 832 (DO_NOT_SAVE_COOKIES | DO_NOT_SEND_AUTH_DATA | DO_NOT_SEND_COOKIES)
                    --> method = "POST"
                    --> privacy_mode = 1
                    --> upload_id = "0"
                    --> url = "http://localhost:3000/api/user/attachment"
t=40044 [st= 33]      NETWORK_DELEGATE_BEFORE_START_TRANSACTION  [dt=0]
t=40044 [st= 33]      HTTP_CACHE_GET_BACKEND  [dt=0]
t=40045 [st= 34]     +HTTP_STREAM_REQUEST  [dt=2]
t=40045 [st= 34]        HTTP_STREAM_JOB_CONTROLLER_BOUND
                        --> source_dependency = 141949 (HTTP_STREAM_JOB_CONTROLLER)
t=40047 [st= 36]        HTTP_STREAM_REQUEST_BOUND_TO_JOB
                        --> source_dependency = 141950 (HTTP_STREAM_JOB)
t=40047 [st= 36]     -HTTP_STREAM_REQUEST
t=40047 [st= 36]      UPLOAD_DATA_STREAM_INIT  [dt=18]
                      --> is_chunked = false
                      --> net_error = 0 (?)
                      --> total_size = 5614
t=40065 [st= 54]     +HTTP_TRANSACTION_SEND_REQUEST  [dt=9]
t=40065 [st= 54]        HTTP_TRANSACTION_SEND_REQUEST_HEADERS
                        --> POST /api/user/attachment HTTP/1.1
                            Host: localhost:3000
                            Connection: keep-alive
                            Content-Length: 5614
                            Accept: application/json, text/plain, */*
                            User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
                            Sec-Fetch-Mode: cors
                            Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryWgG55leMpL8Lg9vl
                            Origin: http://localhost:5000
                            Sec-Fetch-Site: same-site
                            Referer: http://localhost:5000/user/profile
                            Accept-Encoding: gzip, deflate, br
                            Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
t=40065 [st= 54]        HTTP_TRANSACTION_SEND_REQUEST_BODY
                        --> did_merge = false
                        --> is_chunked = false
                        --> length = 5614
t=40065 [st= 54]        UPLOAD_DATA_STREAM_READ  [dt=9]
                        --> current_position = 0
t=40074 [st= 63]        UPLOAD_DATA_STREAM_READ  [dt=0]
                        --> current_position = 5614
t=40074 [st= 63]     -HTTP_TRANSACTION_SEND_REQUEST
t=40074 [st= 63]     +HTTP_TRANSACTION_READ_HEADERS  [dt=420]
t=40074 [st= 63]        HTTP_STREAM_PARSER_READ_HEADERS  [dt=420]
t=40494 [st=483]        HTTP_TRANSACTION_READ_RESPONSE_HEADERS
                        --> HTTP/1.1 200 OK
                            X-Powered-By: Express
                            Content-Type: application/json; charset=utf-8
                            Content-Length: 124
                            ETag: W/"7c-OEPHbRETSP34keHR4wgh/7GupnE"
                            Date: Wed, 14 Aug 2019 13:31:29 GMT
                            Connection: keep-alive
t=40494 [st=483]     -HTTP_TRANSACTION_READ_HEADERS
t=40494 [st=483]      NETWORK_DELEGATE_HEADERS_RECEIVED  [dt=0]
t=40494 [st=483]   -URL_REQUEST_START_JOB
t=40494 [st=483]    URL_REQUEST_DELEGATE_RESPONSE_STARTED  [dt=1]
t=40495 [st=484]    HTTP_TRANSACTION_READ_BODY  [dt=0]
t=40495 [st=484]    URL_REQUEST_JOB_FILTERED_BYTES_READ
                    --> byte_count = 124
t=40495 [st=484]    CANCELLED
t=40495 [st=484] -REQUEST_ALIVE

0 个答案:

没有答案