Value for title can not be cast from ReadablenativeMap to string

Viewed 30148

Consider:

InsertDataToServer = () => {
  const { pinValue1 } = this.state;
  const { pinValue2 } = this.state;
  const { pinValue3 } = this.state;
  const { pinValue4 } = this.state;
  var String_3 = pinValue1.concat(" ", pinValue2);
  var String_4 = String_3.concat(" " , pinValue3);
  var String_5 = String_4.concat(" " , pinValue4);

  fetch("http://www.aonde.biz/mobile/doLogin.php", {
    method: "POST",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      "pin": 212,
    })
  })
    .then(response => response.json())
    .then(responseJson => {
      // Showing the response message coming from
      // the server after inserting records.
      Alert.alert(responseJson);
    })
    .catch(error => {
      console.error(error);
    });

In the above code, when I pass the "pin" parameter API, it shows this error:

In image show full error

How can I resolve this issue?

8 Answers

This error was fixed when the code changed from:

Alert.alert('Error:', error)

to:

Alert.alert('Error:', error.message)

In your Alert.alert(responseJson);, you need to stringify it like this:

Alert.alert(JSON.stringify(responseJson));

Keep in mind that alert is a component from the mobile app, so it might not understand the code shown there is JSON content. If that doesn't work, use responseJson.text().

Make sure your alerts are displaying the error.MESSAGE and not the whole error. Depending on the error and its type that might be the cause!

That was the cause of my issue.

The error was fixed when adding the title in the Alert method.

As in the documentation, the title of Alert must be given while calling the Alert method.

I have had a similar issue. I solved it by changing one of the syntax. In your case, try this:

fetch("http://www.aonde.biz/mobile/doLogin.php",
  method: "POST",
  header: {
    Accept: "application/json",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    "pin":212,

  })
)

What I changed to your coding: I removed {} after the URL.

If you get a red line error on the :, then remove method: header: and body: as well. The code will be like this:

fetch("http://www.aonde.biz/mobile/doLogin.php",
  "POST",
  {
    Accept: "application/json",
    "Content-Type": "application/json"
  },
  JSON.stringify({
    "pin":212,

  })
)

You can check if its string before sending it to Alert.alert method:

const title = "Error title..";
const errorMessage = (typeof error === 'string' || error instanceof String) ? error : error.message;
Alert.alert(title, errorMessage);

I was having this error because I installed a library that needed me to compile the app again and I hadn't compiled it.

After compiling again everything worked fine.

You can simply remove double quotation marks from pin.

fetch("http://www.aonde.biz/mobile/doLogin.php", {
  method: "POST",
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    pin:212,
  })
})
Related