Estoy tratando de OBTENER una respuesta usando la API de búsqueda que no está atascada en un error como mencioné a continuación. Aquí está mi código
const DefaultLayout = () => { let history = useHistory() const callHomePage = async () => { try { const res = fetch('http://localhost:4000/api/authenticate', { method: 'GET', headers: { Accept: 'application/json', 'Content-type': 'application/json', }, credentials: 'include', }) console.log(res) const data = await res.json() console.log(data) if (!res.status === 200) { const error = new Error(res.error) throw error } } catch (err) { console.log(err) history.push('login') } }Error: TypeError: res.json no es una función Promesa {} muestra pendiente
const DefaultLayout = () => { let history = useHistory() const callHomePage = async () => { try { const res = await fetch('http://localhost:4000/api/authenticate', { method: 'GET', headers: { Accept: 'application/json', 'Content-type': 'application/json', }, credentials: 'include', }) console.log(res) const data = await res.json() console.log(data) if (!res.status === 200) { const error = new Error(res.error) throw error } } catch (err) { console.log(err) history.push('login') } }Debe await la declaración de búsqueda y luego llamar al método .json de la respuesta.
const res = await fetch(...) data = await res.json();Leer más: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch