问题
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