I want to send a post request with the request module. And my code snipped like that:
let formData = new FormData();
myArray.map((param, index) => {
formData.append(param.name, param.value)
})
formData.append('file', fs.createReadStream(file.location), { filename: file.name });
var options = {
'method': 'POST',
'url': 'https://shopify-video-production-core-originals.s3.amazonaws.com/',
'headers': {
},
formData: formData,
};
let requestProm = async () => {
return new Promise(resolve => {
request(options, function (error, response) {
if (error) {
logger.error("Video Upload Failed")
console.log(error)
} else {
logger.info("Video Upload Successfull");
resolve(response);
}
})
})
}
await requestProm()
But I getting an error as :
TypeError: Cannot read property 'name' of null
Why Im getting this error? Please Help!
Edit: I controlled my formData. There is no null name value. I searched on internet but I cannot find any solution. So, My formData is like that:
FormData {
_overheadLength: 1031,
_valueLength: 832,
_valuesToMeasure: [
ReadStream {
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 3,
_maxListeners: undefined,
path: '/Users/akasaa101/Desktop/vas-project/flickadoo-shop-service/src/utils/../../temp_files/e43c7a3a37c3703a5e31964942d0f8e09d0d11e3796905320c639356bced17f2-jYX0RlERyX.mp4',
fd: null,
flags: 'r',
mode: 438,
start: undefined,
end: Infinity,
autoClose: true,
pos: undefined,
bytesRead: 0,
closed: false,
emit: [Function (anonymous)],
[Symbol(kFs)]: [Object],
[Symbol(kCapture)]: false,
[Symbol(kIsPerformingIO)]: false
}
],
writable: false,
readable: true,
dataSize: 0,
maxDataSize: 2097152,
pauseStreams: true,
_released: false,
_streams: [
'----------------------------976490531239993416677931\r\n'
'Content-Disposition: form-data; name="bucket"\r\n'
'\r\n',
'shopify-video-production-core-originals',
[Function: bound ],
'----------------------------976490531239993416677931\r\n'
'Content-Disposition: form-data; name="key"\r\n'
'\r\n',
'c/o/v/99de9caee4c04e3e9b46e76d520868f0.mp4',
[Function: bound ],
'----------------------------976490531239993416677931\r\n'
'Content-Disposition: form-data; name="policy"\r\n'
'\r\n',
'eyJjb25kaXRpb25zIjpbWyJlcSIsIiRidWNrZXQiLCJzaG9waWZ5LXZpZGVvLXByb2R1Y3Rpb24tY29yZS1vcmlnaW5hbHMiXSxbImVxIiwiJGtleSIsImMvby92Lzk5ZGU5Y2FlZTRjMDRlM2U5YjQ2ZTc2ZDUyMDg2OGYwLm1wNCJdLFsiY29udGVudC1sZW5ndGgtcmFuZ2UiLDE5MjM0NTcsMTkyMzQ1N10sWyJlcSIsIiRjYWNoZS1jb250cm9sIiwicHVibGljLCBtYXgtYWdlPTMxNTM2MDAwIl0sWyJlcSIsIiR4LWFtei1jcmVkZW50aWFsIiwiQUtJQVlPSTVLWjYySlFDVzYzTFUvMjAyMjAxMTMvdXMtZWFzdC0xL3MzL2F3czRfcmVxdWVzdCJdLFsiZXEiLCIkeC1hbXotYWxnb3JpdGhtIiwiQVdTNC1ITUFDLVNIQTI1NiJdLFsiZXEiLCIkeC1hbXotZGF0ZSIsIjIwMjIwMTEzVDE1NTg0M1oiXV0sImV4cGlyYXRpb24iOiIyMDIyLTAxLTEzVDE2OjU4OjQzWiJ9',
[Function: bound ],
'----------------------------976490531239993416677931\r\n'
'Content-Disposition: form-data; name="cache-control"\r\n'
'\r\n',
'public, max-age=31536000',
[Function: bound ],
'----------------------------976490531239993416677931\r\n'
'Content-Disposition: form-data; name="x-amz-signature"\r\n'
'\r\n',
'9db058a03d62b3ad48c7427e44b60a7d88834b2d67cca5bda792b44d613ab493',
[Function: bound ],
'----------------------------976490531239993416677931\r\n'
'Content-Disposition: form-data; name="x-amz-credential"\r\n'
'\r\n',
'AKIAYOI5KZ62JQCW63LU/20220113/us-east-1/s3/aws4_request',
[Function: bound ],
'----------------------------976490531239993416677931\r\n'
'Content-Disposition: form-data; name="x-amz-algorithm"\r\n'
'\r\n',
'AWS4-HMAC-SHA256',
[Function: bound ],
'----------------------------976490531239993416677931\r\n'
'Content-Disposition: form-data; name="x-amz-date"\r\n'
'\r\n',
'20220113T155843Z',
[Function: bound ],
'----------------------------976490531239993416677931\r\n'
'Content-Disposition: form-data; name="file"; filename="jYX0RlERyX.mp4"\r\n'
'Content-Type: video/mp4\r\n'
'\r\n',
DelayedStream {
source: [ReadStream],
dataSize: 0,
maxDataSize: Infinity,
pauseStream: true,
_maxDataSizeExceeded: false,
_released: false,
_bufferedEvents: [Array],
_events: [Object: null prototype],
_eventsCount: 1
},
[Function: bound ]
],
_currentStream: null,
_insideLoop: false,
_pendingNext: false,
_boundary: '--------------------------976490531239993416677931'
}
CodePudding user response:
Check the type of myArray elements
CodePudding user response:
Cannot read property '' of null means that the property of the object doesn't exist. What you can do is log file and then check if the property 'name' exist.
Remember: console.log() is your best friend in nodejs (and in programming in general)
