Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(React-Native) undefined is not a constructor evaluating 'new FormData()'

for the past 2 days I struggled with this problem and can't figure it out. I don't understand this error. this is my code:

try{
var formData = new FormData();
} catch (error) {
console.error('FormData ERROR', error);
}

and this is the error:

017-06-21 13:49:02.761 [error][tid:com.facebook.React.JavaScript] 'FormData ERROR', { [TypeError: undefined is not a constructor (evaluating 'new FormData()')] line: 98419, column: 36, sourceURL: 'http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false' }

Do i have to add support for FormData in ReactNative 0.45.0? please help

like image 762
Cristi Milea Avatar asked Jun 21 '17 11:06

Cristi Milea


3 Answers

I got into this problem as well and that's because I have imported them on top. If you have done like what I did:

import { FormData } from 'react';

or

import { FormData } from 'react-native';

Just remove "FormData" from your imports and it will work like magic. FormData doesn't have to be imported for it to be working.

like image 89
Cool Yeah Avatar answered Nov 18 '22 04:11

Cool Yeah


The error says that you are FormData is undefined. Since it is not part of react native, you probably have to download it from npm.

npm install --save form-data

Then import it at the top of your file

import FormData from 'form-data';
like image 37
Mμ. Avatar answered Nov 18 '22 04:11

Mμ.


if

import { FormData } from 'react-native';

didn't help then you can try to import directly:

import FormData from 'react-native/Libraries/Network/FormData';
like image 25
Viktor Avatar answered Nov 18 '22 04:11

Viktor