简体   繁体   English

\"path\" 参数必须是字符串类型或 Buffer 或 URL 的实例。 从 nodejs 收到 undefined

[英]The \"path\" argument must be of type string or an instance of Buffer or URL. Received undefined from nodejs

I am trying to get a file from reactjs and send as param to Node.js backend for processing in an API.我正在尝试从 reactjs 获取文件并作为参数发送到 Node.js 后端,以便在 API 中进行处理。 But it gives me this error.但它给了我这个错误。 Here is the code:这是代码:

const FormData = require("form-data");
    const fs = require("fs");
    const model = "tr-8";
    const formd = new FormData();
    const { voice } = req.body;
    const file = voice;
    const timeout = 360000;
    formd.append("model", model);
    formd.append("files[]", fs.createReadStream(voice, { autoClose: true }));
    const request = formd.submit(
      `${Process.env.API}` + model,
      function (err, rs) {
        if (err) {
          console.log("formd.submit error " + voice);

          console.log(err);
        }

        if (rs) {
          var resp = Buffer.from([]);

          rs.on("error", function (err) {
            console.log("formd.submit on error " + voice);

            console.log(err);
          });

          rs.on("close", function () {
            console.log("close " + voice);
          });

          rs.on("data", function (chunk) {
            resp = Buffer.concat([resp, chunk]);
          });

          rs.on("end", function () {
            console.log("end " + voice);

            const resputf8 = resp.toString("utf8");

            const recognitionResult = JSON.parse(resputf8);

            const speech = recognitionResult.JsonResult;

            if (speech.error) {
              console.log("transcript error " + speech.error + " " + voice);
            } else {
              console.log(JSON.stringify(speech, null, 2));
            }
          });
        }
      }
    );

And In the frontend, I send the file like that:在前端,我这样发送文件:

<Input type="file" name="voice" />

This is my error code:这是我的错误代码:

    at ReadStream._construct (node:internal/fs/streams:64:17)
    at constructNT (node:internal/streams/destroy:288:25)
    at processTicksAndRejections (node:internal/process/task_queues:80:21) {
  code: 'ERR_INVALID_ARG_TYPE'

How can i prevent this error?我怎样才能防止这个错误?

You need multer for multipart files and uploads.您需要 multer 进行多部分文件和上传。 In frontend add this在前端添加这个

<form action="/voice" method="post" enctype="multipart/form-data">
  <input type="file" name="voice" />
</form>

and in backend,在后端,

var multer  = require('multer')
var upload = multer({ dest: 'uploads/' })

app.post('/voice', upload.single('voice'), function (req, res, next) {
    // req.file is the `voice` file
    const  voice  = req.file
    // req.body will hold the text fields, if there were any
})

暂无
暂无

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

相关问题 fs.FileRead -&gt; TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串、缓冲区或 URL 类型之一。 接收类型未定义 - fs.FileRead -> TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be one of type string, Buffer, or URL. Received type undefined 第一个参数必须是字符串类型或缓冲区或 uint8array 的实例。 收到未定义 - First argument must be of type string or an instance of buffer or uint8array. Received undefined Nodejs:[ERR_INVALID_ARG_TYPE]:“data”参数必须是string类型或者Buffer的实例等收到Array的实例 - Nodejs: [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, etc. Received an instance of Array ytdl:“url”参数必须是字符串类型。 接收类型未定义 - ytdl: The "url" argument must be of type string. Received type undefined “chunk”参数必须是字符串类型或 Buffer 的实例 - The "chunk" argument must be of type string or an instance of Buffer TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 收到 Object 的实例 - TypeError [ERR_INVALID_ARG_TYPE]: The “path” argument must be of type string. Received an instance of Object axios “url”参数必须是字符串类型。 收到类型未定义错误 - axios The "url" argument must be of type string. Received type undefined error mongoose findbyId() 显示“路径”参数必须是字符串类型。 收到模型的实例&#39; - mongoose findbyId() showing 'The "path" argument must be of type string. Received an instance of model' 错误:“路径”参数必须是字符串类型。 收到未定义。 firebase deploy --only 功能 - Error: The “path” argument must be of type string. Received undefined. firebase deploy --only functions (hashlips_art_engine-1.1.2_patch_v5) “路径”参数必须是字符串类型。 收到未定义 - (hashlips_art_engine-1.1.2_patch_v5) The "path" argument must be of type string. Received undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM