Reputation: 355
am new in react native and am trying to fetch book api , while fetching api getting an Warning like Unhandle Promises Rejection TypeError Network Request failed, don't know where am wrong, please try to fix my error if you have ay query please free feel to ask any time
import axios from 'axios';
import React, { useEffect } from 'react'
import { Text, View} from 'react-native';
import { Card, Title, Paragraph } from 'react-native-paper';
import { Searchbar } from 'react-native-paper';
export default function Home() {
const [searchquery, setSearchquery] = React.useState();
const bookUrl = "AIzaSyBlFjUCkaUPmmRdglEO-%%%%%%%%%%%%%%5";
const getData = async ()=>{
const response = await fetch(bookUrl);
const data = await response.json();
console.log(data);
}
useEffect(() => {
getData()
}, []);
// useEffect(()=>{
// async function getData(){
// const res = await axios.get(`AIzaSyBlFjUCkaUPmmRdglEO-7FbzerUtCJEtEk${searchquery}`)
// console.log(res);
// }
// getData()
// },[])
return (
<View >
<Searchbar
placeholder="Search Books"
onChangeText={(query) => setSearchquery(query)}
value={searchquery}
style={{ marginTop: 30, marginHorizontal: 10 }}
/>
</View>
);
}
Upvotes: 0
Views: 177
Reputation: 1097
try it like this:
const getData = async () => {
let response, data;
try {
response = await fetch(bookUrl);
data = await response.json();
console.log(data);
} catch (error) {
console.log('could not fetch data. url: ' + bookUrl + ' error: ' + JSON.stringify(error));
}
}
Upvotes: 1