Reputation: 209
import React from 'react';
import { FlatList, ActivityIndicator, Text, View } from 'react-native';
export default class FetchExample extends React.Component {
constructor(props){
super(props);
this.state ={ isLoading: true}
}
componentDidMount(){
fetch('https://apitest.kuveytturk.com.tr/prep/v1/data/fecs', {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
firstParam: 'isoCode ',
secondParam: 'internationalCode',
thirdParam: 'name',
fourthParam: 'code',
FifthParam: 'group',
SixthParam: 'id'
}),
});
}
render(){
if(this.state.isLoading){
return(
<View style={{flex: 1, padding: 20}}>
<ActivityIndicator/>
</View>
)
}
return(
<View style={{flex: 1, paddingTop:20}}>
<FlatList
data={this.state.dataSource}
renderItem={({item}) => <Text>{item.isoCode}, {item.internationalCode}, {item.name}, {item.code}, {item.group}</Text>}
keyExtractor={({id}, index) => id}
/>
</View>
);
}
}
I am the beginner of react native and i am trying to get data from api but i have an error about code.
possible unhandled promise rejection (id 0) typeerror undefined is not an object
I can't understand why there is no object
Hope for your help.
Upvotes: 4
Views: 9268
Reputation: 197
You can't use body in GET request. Although you can convert your JSON object to URL query parameters like this.
let url += '?'+Object.keys(params).map((k) => (k + '=' + encodeURIComponent(params[k]))).join('&')
Upvotes: 0
Reputation: 114
You can not use body with GET request. If you want to pass params with GET request then the official way to work with query parameters is just to add them onto the URL. This is an example:
var url = new URL("https://apitest.kuveytturk.com.tr/prep/v1/data/fecs"),
params = {firstParam: 'isoCode ',
secondParam: 'internationalCode',
thirdParam: 'name',
fourthParam: 'code',
FifthParam: 'group',
SixthParam: 'id'}
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]))
fetch(url).then(/* … */)
Upvotes: 3
Reputation: 10604
Please note that you cannot use body in GET Request. Please change it to POST.
Upvotes: 6