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:.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
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:.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
Share Improve this question edited Mar 5, 2018 at 12:45 David Schumann 14.8k13 gold badges83 silver badges105 bronze badges asked Jun 21, 2017 at 11:03 Cristi MileaCristi Milea 911 gold badge2 silver badges7 bronze badges 4- FormData is undefined, can you make sure that the object exists in your code? – Mμ. Commented Jun 21, 2017 at 11:47
- @D-reaper how do i do that? FormData i think is included in react-native / libraries / network – Cristi Milea Commented Jun 21, 2017 at 12:30
- console.log(FormData). If it is undefined, then you just have to make sure that you include it in your code. – Mμ. Commented Jun 21, 2017 at 12:31
- after console.log(FormData) i get undefined. i tried import { FormData } from 'react-native'; but still no luck – Cristi Milea Commented Jun 21, 2017 at 12:47
4 Answers
Reset to default 5I 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.
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';
if
import { FormData } from 'react-native';
didn't help then you can try to import directly:
import FormData from 'react-native/Libraries/Network/FormData';
I think I got it.
First of all, I found something (don't know from where i had these "tips") in my index.ios.js and removed them
// const _XHR = GLOBAL.originalXMLHttpRequest ?
// GLOBAL.originalXMLHttpRequest :
// GLOBAL.XMLHttpRequest
// XMLHttpRequest = _XHR;
// // FormData = global.originalFormData;
Then I found out that my debugger (Chrome or RNDebugger) messed with my network requests and that was bad for multipart image object. (strange?!)
finally i got it working with this code:
var fdObject = new FormData();
fdObject.append('avatar', {uri: PicturePath, name: 'avatar.jpg', type: 'image/jpg'});
options.method = POST;
options.headers['Content-Type'] = 'multipart/form-data';
options.body = fdObject;
fetch("http://api./post-my-image", options);