form-data library throwing cannot read property of null error when trying append attachments to send via request.post

孤街浪徒 提交于 2020-02-05 04:01:11

问题


When I try to make a request.post with form-data for attachments I am getting the following error. The error is coming from the form-data library.

var filename = options.filename || value.name || value.path;
                                          ^
TypeError: Cannot read property 'name' of null

This is what my form construction and appending looks like. I am basically attaching a JSON Web Token and an image file.

var formData = new FormData();
formData.append('jwt', jwt);

var imgToAttach = fs.createReadStream(path.join(__dirname, 'abc.jpg'));
formData.append('attachmentA', imgToAttach);

sendRequest(formData);

The sendRequest function looks like this.

    function sendRequest(formData) {
      var reqOptions = {
        url: options.host, 
        formData: formData,
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      };
      request.post(reqOptions, function (err, resp) {...}

I've been trying to resolve this issue for a while but nothing has worked. Any help much appreciated.


回答1:


For some reason, request module can't get the name from form-data

Try this instead:

const formData = {
    attachmentA: {
      value: imgToAttach,
      options: {
        filename: "abc",
        contentType: "jpg"
      }
    }
};


来源:https://stackoverflow.com/questions/44597174/form-data-library-throwing-cannot-read-property-of-null-error-when-trying-append

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!