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

I have been struggling with this issue for the last 2 days and cannot 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 need to add FormData support to ReactNative 0.45.0? please, help

+3


source to share


3 answers


I think I got it.

First of all, I found something (don't know where I got these "tips" from) 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 up my network requests and that was bad for a multi-threaded image object. (Strange ?!)

Finally, I started 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.com/post-my-image", options);

      

0


source


I got into this problem too, and because I imported them from above. If you did what I did:

import { FormData } from 'react';

      

or



import { FormData } from 'react-native';

      

Just remove "FormData" from your import and it will work like magic. FormData does not need to be imported for it to work.

+2


source


The error says you are FormData 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 beginning of the file

import FormData from 'form-data';

      

0


source







All Articles