“ string”参数必须是string,Buffer或ArrayBuffer类型之一。收到的类型对象

时间:2020-08-20 07:52:27

标签: javascript node.js express

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

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;'

1 个答案:

答案 0 :(得分:0)

从外观上看,您正在使用superagent执行上传。由于您打算进行multipart/form-data上传,因此无法使用.send()。来自docs

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

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

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

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