POST fails with ReadableNativeMap cannot be cast to String error

11,489

Alert.alert receives an string, and what you're getting from the fetch response is internally a com.facebook.react.bridge.ReadableNativeMap object (which is what the native implementation for fetch returns). You can try:

Alert.alert(JSON.stringify(responseJson))

If you were using iOS you'll get a completely different error:

Exception '-[_NSFrozenDictionaryM length]: unrecognized selector  ...
Share:
11,489
Berke
Author by

Berke

https://www.linkedin.com/in/m-berke-yalçın-09814571/

Updated on August 20, 2022

Comments

  • Berke
    Berke almost 2 years

    I'm working in React Native, I use PHP for backend and when I use fetch POST request I get so strange error, and I dont know why it happens. I checked the url so it works no problem, also normal fetch() is working without POST but when I try to post it happens. When I try it in local server fetch POST works.. but in server, I get this error :

    ERROR : com.facebook.react.bridge.ReadableNativeMap cannot be cast to java.lang.String

    React native codes :

    fetch('http://xxx/react_test1', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        name: inputName,
        email: inputEmail,
        phone: inputPhone
      }),
    }).then((response) => response.json())
      .then((responseJson) => {
        Alert.alert(responseJson);
      }).catch((error) => {
      alert(error);
    });
    
  • Berke
    Berke about 6 years
    ok i will check alert part of the code and change it and let you know .
  • Berke
    Berke about 6 years
    I checked now it gives other error but inside of alert, so we fixed this one. new Error is : {"message":"",exception":"Symfony\\Component\\HttpKernel...
  • Berke
    Berke about 6 years
    I Think its about Laravel / PHP part, so I need to fix there. then thank you again!