首页 > 解决方案 > “string”参数必须是 string、Buffer 或 ArrayBuffer 类型之一。接收到的类型对象

问题描述

在给定的代码中出现此错误。我在文件上传时调用此方法,它将捕获块。

ERR: TypeError [ERR_INVALID_ARG_TYPE]: The "string" argument must be one of type string, Buffer, or ArrayBuffer. Received type object

        try{
          request
            .post(url)
            .set(postHeaders)
            .send(postData)
            .end(function (err, response) {
                if(err) {
                    console.log(err);
                    res.send(errorJson);
                    return;
                }
                res.set(response.header);
                res.send(response.text);
            });
          } catch(err){
              console.log("error" , err)
          } 

标题是

'content-type': 'multipart/form-data;'

标签: javascriptnode.jsexpress

解决方案


从外观上看,您正在使用它superagent来执行上传。由于您打算进行multipart/form-data上传,因此不能使用.send(). 从文档

多部分请求 SuperAgent 也非常适合构建它提供方法 .attach() 和 .field() 的多部分请求。

当您使用 .field() 或 .attach() 时,您不能使用 .send() 并且不能设置 Content-Type (将为您设置正确的类型)

因此,您需要将代码更改为:

request
       .post(url)            
       .field(<setYourFieldDataHere>) // or use .attach() if you want to upload files
       //

推荐阅读