11

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Atul Tiwari
  • 544
  • 1
  • 7
  • 17

8 Answers8

33

This error was fixed when the code changed from:

Alert.alert('Error:', error)

to:

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bill Zelenko
  • 2,606
  • 1
  • 17
  • 26
  • What is the explanation for it? Please respond by [edit]ing (changing) your question/answer, not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen May 29 '22 at 10:54
9

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().

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nicolas Silva
  • 566
  • 1
  • 10
  • 28
2

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.

Marlon Englemam
  • 351
  • 5
  • 11
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ömer
  • 188
  • 2
  • 11
0

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,

  })
)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lolz
  • 51
  • 1
  • 2
  • Re *"I removed {} after the URL."*: ***Why*** does that work? What is the explanation? From [the Help Center](https://stackoverflow.com/help/promotion): *"...always explain why the solution you're presenting is appropriate and how it works"*. Please respond by [edit]ing (changing) your answer, not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen May 29 '22 at 11:02
0

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

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.

-2

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,
  })
})
Samitha Nanayakkara
  • 2,529
  • 2
  • 10
  • 23
  • I don't understand why you are only adding 212 as your pin even after you concatenate all the strings. However when I check with given URL and body as I suggested, I got status:1 output. I don't understand why you are saying the issue is occurring. If you can be more elaborate, I can help you. – Samitha Nanayakkara Oct 26 '18 at 16:40
  • can you share your code through you can get status 1? Thank you – Atul Tiwari Oct 27 '18 at 10:33
  • I got the Status 1 using postman. But with the method I used in this answer, I did not get the error you are saying because you don't have to put key in double quotes when you are using stringify. Only the value if it is not numeric. – Samitha Nanayakkara Oct 28 '18 at 12:22