kutirie
kutirie

Reputation: 641

com.facebook.react.bridge.readablenativemap cannot be cast to java.lang.string

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.

Upvotes: 31

Views: 71111

Answers (8)

Oben Desmond
Oben Desmond

Reputation: 171

I got this same error

com.facebook.react.bridge.UnexpectedNativeTypeException: Value for message cannot be cast from ReadableNativeMap to String

I had somewhere I was doing

Alert.alert(error)

This error was coming from an endpoint I was trying to access, it seems like this error wasn't a string.

I was able to just quickly convert Alert.alert(error) into console.log(error)

I could see what the actual error was

If you want to really alert this then you can do

Alert.alert(JSON.stringify(error))

Upvotes: 0

Yuniac
Yuniac

Reputation: 583

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.

Upvotes: 3

Ravi Raja Jangid
Ravi Raja Jangid

Reputation: 935

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));

Upvotes: 0

Gowtham V
Gowtham V

Reputation: 33

react-native link

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

Note : Only for react-native < 60.x

Upvotes: -3

Rishav Kumar
Rishav Kumar

Reputation: 5460

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

Upvotes: 4

Shubham1164
Shubham1164

Reputation: 357

I was getting this error because my URL was incorrect

Upvotes: 2

anjaneyulubatta505
anjaneyulubatta505

Reputation: 11695

In my case I was not running the api server.

Upvotes: 0

kutirie
kutirie

Reputation: 641

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

Upvotes: 27

Related Questions