Unexpected end of input when using fetch API with react hooks

Viewed 174

i'm trying to fetch JSON from the give API: https://dummy-api.com/api/drivers

i've included mode: no-cors to the fetch() function. However, now i've gotten

Unexpected end of input

which doesn't fetch anything.

I've also tried doing res.text() before parsing as JSON and it didn't work either.

Here are my codes:

const [isLoading, setIsLoading] = useState(false)
const [drivers, setDrivers] = useState([])
const [error, setError] = useState(null)

useEffect(() => {
        const fetchData = async () => {
            try {
                const response = await fetch(
                    'https://dummy-api.com/api/drivers',
                    {
                        mode: 'no-cors'
                    }
                )
                const data = await response.json()
                console.log(data)
            } catch (error) {
                setIsLoading(true)
                setError(error)
                console.log(error.message)
            }
        }
        fetchData()
    }, [])

so the output im supposed to see should be a JSON form like this:

{"pickup_eta":6,"drivers":[{"driver_id":"0-ikkpyhyjg9","location":{"latitude":null,"longitude":null,"bearing":229}}]}

thank you!

0 Answers
Related