简体   繁体   English

NodeJS - Apollo Server:在文件上传流解析期间请求断开连接

[英]NodeJS - Apollo Server: Request disconnected during file upload stream parsing

I am sending a file from the client to apollo-server which will then make another request to service with the file.我正在从clientapollo-server发送一个文件,然后它会再次请求使用该文件提供服务。

Here's an example of the code:下面是代码示例:

import request from "request"

uploadFile: async (_, { file } => {
  const { filename, createReadStream } = await file
  const stream = createReadStream()

  const options = {
    method: "POST",
    url: "http://localhost:5000/api/upload",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
    formData: {
      file: {
        value: stream,
        options: {
          filename,
          contentType: null,
        },
      },
    },
  }

  request(options, err => console.log(err))
})

Which results in the following error:这导致以下错误:

BadRequestError: form-data: Request disconnected during file upload stream parsing.
    at IncomingMessage.abort (/Users/hansel/Documents/LIT/Development/creators-app/server/node_modules/graphql-upload/lib/processRequest.js:89:33)
    at Object.onceWrapper (events.js:417:28)
    at IncomingMessage.emit (events.js:311:20)
    at IncomingMessage.EventEmitter.emit (domain.js:482:12)
    at resOnFinish (_http_server.js:685:7)
    at ServerResponse.emit (events.js:323:22)
    at ServerResponse.EventEmitter.emit (domain.js:482:12)
    at onFinish (_http_outgoing.js:730:10)
    at onCorkedFinish (_stream_writable.js:721:5)
    at afterWrite (_stream_writable.js:528:5)
    at afterWriteTick (_stream_writable.js:515:10)
    at processTicksAndRejections (internal/process/task_queues.js:83:21) {
  message: 'form-data: Request disconnected during file upload stream parsing.',
  expose: true,
  statusCode: 499,
  status: 499
}

The file is a valid readable stream.file是一个有效的可读流。 The error only occurs when I try to send it in a request.该错误仅在我尝试在请求中发送时发生。

If I replace the stream with fs.createReadStream("filePath") the request works fine.如果我用fs.createReadStream("filePath")替换stream ,则请求工作正常。

Anyone ran into this problem?有人遇到过这个问题吗?

Try to await your async uploadFile in resolver, for example:尝试在解析器中等待您的异步上传文件,例如:

Mutation:{
uploadFile:async (_, { file }, { uploadFile}) => {
  await uploadFile(file)
}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM