4

sorry for the silly question. I am relatively new to react native. I have got fetch working so I can get the json response back from the server. The server API returns a string if there is an error or returns an json object if its successful. is there any way to compare the response to see if its a string or json variable?

Not sure how to achieve the above any help would be appreciated.

here is my code

API.js

var API = {

   SetupBuyOnline(email, serialNumber, platformType) {
        var url = 'https://<urlomitted>/SetupBuyOnline';
        return fetch(url, {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json; charset=UTF-8',
                'Accept': 'application/json',
                'operating_system': platformType,
                'email': email,
                'serialNumber': serialNumber
            }
        }).then((res) => res.json());
    }
};

find userScreen.js

  findUserScreen(){
    // this.props.navigation.navigate('JoinNowScreen')
    StudentApi.SetupBuyOnline('[email protected]', deviceId, "iOS")
    .then((responseData) => {
      if(typeof(responseData) == 'string')
      {
        console.log('got api call ' + responseData);
        alert('test = ' + responseData);
      }
      else
      {
        console.log('got api call ' + responseData);
        alert(responseData);
      }
    })
  }

Not sure what I am doing wrong. Thanks in advance

1 Answer 1

5

Other than changing your == for === (strict equality) not much. But will work with normal equality.

Dont know what you're doing wrong, because this code works just fine.

What I would do instead is having a JSON response like so:

"{
    error: true,
}"


"{
    error: false,
    data: yourDataSentByTheServer!
}"

That way you only need to check if there's an error property inside your JSON response.

function stringType(){
  const responseData = 'hello';
  if(typeof(responseData) === 'string')
  {
    console.log('got api call ' + responseData);
    alert('test = ' + responseData);
  }
  else
  {
    console.log('got api call ' + responseData);
    alert(responseData);
  }
}

function objType(){
  const responseData = { 1:'hello', 2: 'koko' }
  if(typeof(responseData) === 'string')
  {
    console.log('got api call ' + responseData);
    alert('test = ' + responseData);
  }
  else
  {
    console.log('got api call ' + responseData);
    alert(responseData);
  }
}
<button onClick="stringType();">Click me for string</button>
<button onClick="objType();">Click me for obj</button>

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

2 Comments

weird. when I tried before it didnt detect it as a string. Anyway thanks for your help :)
Alright :) but consider my option for your JSON, its a good approach to keep things concise :)

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.