28

My code:

const file = {
  uri: this.state.imageSource,
  name: this.state.imageName,
  type: 'image/jpg',
};

const data = new FormData();
data.append('file', file);

fetch(config.server + '/upload', {
  method: 'POST',
  body: data,
})
  .then((res) => res.json())
  .then((responseData) => {
    alert(JSON.stringify(responseData));
  })
  .catch((err) => {
    alert(err);
  });

Without FormData code doesnt display error. What I should do to fix? Debugging on android.

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
kutirie
  • 583
  • 1
  • 4
  • 9

7 Answers7

24

The error was because I was passing in the url as an array but it expected a string.

MattyK14
  • 2,046
  • 2
  • 17
  • 29
kutirie
  • 583
  • 1
  • 4
  • 9
  • 3
    Got the same error when passing an object where a string was required. Can be stringified with `JSON.stringify(theobject)`. May not be appropriate in this case of an array of URLs though. – Jeppe Jun 03 '19 at 09:13
4

This will resolve the issue : alert(JSON.stringify(data))

Rishav Kumar
  • 4,979
  • 1
  • 17
  • 32
2

I was getting this error because my URL was incorrect

Shubham1164
  • 357
  • 6
  • 16
0

In my case I was not running the api server.

anjaneyulubatta505
  • 10,713
  • 1
  • 52
  • 62
0

In case this is helpful to someone, I was getting this error:

com.facebook.react.bridge.readableNativeArray cannot be cast to java.lang.boolean

And the issue was me passing a string instead of a boolean to a component.

Yuniac
  • 343
  • 3
  • 11
-1

I also faced the same issue and the below line of code can be a solution.

var formdata = new FormData();
formdata.append("mobile", phonenumber);

var requestOptions = {
method: 'POST',
body: formdata,
redirect: 'follow'
};

fetch("http://your_url", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
Ravi Raja Jangid
  • 795
  • 9
  • 16
-2
react-native link

Use this command to resolve this issue. This worked for me.

Note : Only for react-native < 60.x

Gowtham V
  • 27
  • 6