octet-stream / form-data

Spec-compliant FormData implementation for Node.js
https://www.npmjs.com/package/formdata-node
MIT License
142 stars 17 forks source link

[ISSUE] in function scope #34

Closed Mansi1 closed 3 years ago

Mansi1 commented 3 years ago

Issue in function scope got this problem, writting a new fetch implementation.

//this happend because i use destuctors like
const { getComputedLenght } = formData;
(node:13060) DeprecationWarning: To create a stream from the instance use Readable.from(formData) instead
(node:13060) UnhandledPromiseRejectionWarning: TypeError: undefined is not iterable (cannot read property Symbol(Symbol.ite
rator))
    at getComputedLength (C:\Users\Mansi\Documents\projects\ng-fetch\node_modules\formdata-node\lib\cjs\FormData.js:187:22)

fix use arrow function

getComputedLength()  => { ...  this is defined}
octet-stream commented 3 years ago

Hello. This is not a bug, it's just how this works in JavaScript. And I meant this method to have such behaviour.

According to your code:

https://github.com/Mansi1/ng-fetch/blob/ec2ad07a02aa15f7fb9aa8c2b538491f08133294/src/runtime/node/ng-fetch-node-builder.ts#L31

You trying to get a reference to getComputedLength and put it to a constant. This is where the method gets unbound from FormData instance. However, the stream is property created in FormData constructor, so it keeps a reference to FormData instance. Similar for headers - it is getter, so you'll have its result once you access it. To fix your problem, you need to call getComputedLength right from the FormData instance, or bind it first:

const contentLength = bodyToSend.getComputedLength()

// or

const getComputedLength = bodyToSend.getComputedLength.bind(bodyToSend)

I think the last one would be unnecessary for your use-case.