0

Here is my code.

I am calling the following function to get state list.

 callGetStatesApi()
 {
   callGetApi(GLOBAL.BASE_URL + GLOBAL.Get_States)
   .then((response) => {
           // Continue your code here...
           stateArray = result.data
           Alert.alert('Alert!', stateArray)
     });
 }

Here is common callGetApi function for getting response from GET Api.

export function callGetApi(urlStr, params) {
    return fetch(urlStr, {
            method: "GET",
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(params)
        })
        .then((response) => response.json())
        .then((responseData) => {
            result = responseData
        })
        .catch((error) => {
             console.error(error);
             Alert.alert('Alert Title failure' + JSON.stringify(error))
        });
} 

I am getting the following error.

enter image description here

1 Answer 1

1

Alert only show the string , but your case "stateArray" is complex object (array,structure..)

So use stateArray.toString() or JSON.stringify(stateArray),

Or

Try the below method and let me know,

fetch(GLOBAL.BASE_URL + GLOBAL.Get_States, {
    method: 'get',
    headers: { 'Accept': 'application/json','Content-Type': 'application/json',}
}).then((response) => response.json())
.then((responseData) => {
    console.log(responseData) // this is the response from the server
    // Continue your code here...
       stateArray = result.data
       Alert.alert('Alert!', stateArray)
}).catch((error) => {
   console.log('Error');
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was looking for the following. "use stateArray.toString() or JSON.stringify(stateArray)".

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.