3
  • When I filter the products with respect to category and showing products in ViewProducts.js, I am getting this issue.
  • When I filter the products with respect to category and only showing in the alert in categiry.js it is showing me all the fetched filtered data in the alert box.
  • Whereas when I am not filtering products with respect to the category, it is showing me all the products with no error.

Below is the code for fething in ViewProducts.js

  componentDidMount() {
  return fetch('http://192.168.0.109/fyp/products.php')
    .then((response) => response.json())
    .then((responseJson) => {
      let ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
      this.setState({
        isLoading: false,
        dataSource: ds.cloneWithRows(responseJson),
      }, function() {
      });
    })
    .catch((error) => {
      console.error(error);
    }); 
}

enter image description here

3
  • 3
    An example of the JSON content you get when the error occurs would help. Commented Feb 8, 2018 at 8:47
  • Yes that would be good. Or you could just check if your response string is a valid JSON Commented Feb 8, 2018 at 9:39
  • In alert it is fetching the data while showing it is giving me the error. Commented Feb 8, 2018 at 10:00

2 Answers 2

2

This usually happen if you are having an error and its not json_encoded. Try catching all the errors you are receiving and echo Json response For instance mysql insert error could be handled as below

if ($conn->query($sql) === TRUE) {

    // If the record inserted successfully then show the message.
    $MSG = "New record created successfully";

// Converting the message into JSON format.

    $json = json_encode($MSG);

// Echo the message.
    echo $json;

} else {
    $errorMsg="Error: " . $sql . "<br>" . $conn->error;
    $json = json_encode($errorMsg);
    echo $json;
}

Otherwise you can share your server code, will help troubleshoot!

Sign up to request clarification or add additional context in comments.

Comments

0

This is because the response you are getting is not in the proper JSON format, so before cloning it to list view do console.log or console.warn of fetch(URL)then(response)=>response.text() and print here then if you are getting JSON means then parse that or else show some snack enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.