skip to Main Content

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/usr/Desktop/vas-project/prj-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: [
    '----------------------------976490531239993416677931rn' +
      'Content-Disposition: form-data; name="bucket"rn' +
      'rn',
    'shopify-video-production-core-originals',
    [Function: bound ],
    '----------------------------976490531239993416677931rn' +
      'Content-Disposition: form-data; name="key"rn' +
      'rn',
    'c/o/v/99de9caee4c04e3e9b46e76d520868f0.mp4',
    [Function: bound ],
    '----------------------------976490531239993416677931rn' +
      'Content-Disposition: form-data; name="policy"rn' +
      'rn',
    'eyJjb25kaXRpb25zIjpbWyJlcSIsIiRidWNrZXQiLCJzaG9waWZ5LXZpZGVvLXByb2R1Y3Rpb24tY29yZS1vcmlnaW5hbHMiXSxbImVxIiwiJGtleSIsImMvby92Lzk5ZGU5Y2FlZTRjMDRlM2U5YjQ2ZTc2ZDUyMDg2OGYwLm1wNCJdLFsiY29udGVudC1sZW5ndGgtcmFuZ2UiLDE5MjM0NTcsMTkyMzQ1N10sWyJlcSIsIiRjYWNoZS1jb250cm9sIiwicHVibGljLCBtYXgtYWdlPTMxNTM2MDAwIl0sWyJlcSIsIiR4LWFtei1jcmVkZW50aWFsIiwiQUtJQVlPSTVLWjYySlFDVzYzTFUvMjAyMjAxMTMvdXMtZWFzdC0xL3MzL2F3czRfcmVxdWVzdCJdLFsiZXEiLCIkeC1hbXotYWxnb3JpdGhtIiwiQVdTNC1ITUFDLVNIQTI1NiJdLFsiZXEiLCIkeC1hbXotZGF0ZSIsIjIwMjIwMTEzVDE1NTg0M1oiXV0sImV4cGlyYXRpb24iOiIyMDIyLTAxLTEzVDE2OjU4OjQzWiJ9',
    [Function: bound ],
    '----------------------------976490531239993416677931rn' +
      'Content-Disposition: form-data; name="cache-control"rn' +
      'rn',
    'public, max-age=31536000',
    [Function: bound ],
    '----------------------------976490531239993416677931rn' +
      'Content-Disposition: form-data; name="x-amz-signature"rn' +
      'rn',
    '9db058a03d62b3ad48c7427e44b60a7d88834b2d67cca5bda792b44d613ab493',
    [Function: bound ],
    '----------------------------976490531239993416677931rn' +
      'Content-Disposition: form-data; name="x-amz-credential"rn' +
      'rn',
    'AKIAYOI5KZ62JQCW63LU/20220113/us-east-1/s3/aws4_request',
    [Function: bound ],
    '----------------------------976490531239993416677931rn' +
      'Content-Disposition: form-data; name="x-amz-algorithm"rn' +
      'rn',
    'AWS4-HMAC-SHA256',
    [Function: bound ],
    '----------------------------976490531239993416677931rn' +
      'Content-Disposition: form-data; name="x-amz-date"rn' +
      'rn',
    '20220113T155843Z',
    [Function: bound ],
    '----------------------------976490531239993416677931rn' +
      'Content-Disposition: form-data; name="file"; filename="jYX0RlERyX.mp4"rn' +
      'Content-Type: video/mp4rn' +
      'rn',
    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'
}

2

Answers


  1. 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)

    Login or Signup to reply.
  2. Apparently, form-data is already handled by request-promise. You can pass the form-data as an object. Check this

    https://stackoverflow.com/a/59530483/17357155

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search