Monu Patil
Monu Patil

Reputation: 355

Unhandle Promises Rejection TypeError Network Request failed

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

getting an warning like

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

Answers (1)

YaNuSH
YaNuSH

Reputation: 1097

  1. It seems you are trying to fetch from an invalid url "AIzaSyBlFjUCkaUPmmRdglEO-%%%%%%%%%%%%%%5"
  2. You're importing axios but still using regular fetch. Is this what you were intended to do?
  3. When using fetch, things can go wrong (with invalid urls for example). so you need to wrap your fetch with try catch in order to give a fallback to whenever the action fails. try something like this:

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

Related Questions