Voy a usar Axios para comunicar la API. Pero ese tipo de error sigue saliendo. No entiendo este problema. Busqué en Internet y probé de todo. Ayúdame. Todo lo que quiero es hacer clic en ese botón para ver el valor bajo en la herramienta de desarrollo.
useEffect(() => { setJwt(getClientCookieFromClient('jwt')); }, []); const customFetch = async () => { const res = await axios .get(`${process.env.NEXT_PUBLIC_WECODE_URI}/subscription/master_table`, { headers: { Authentication: jwt, }, }) .then((res) => res.data); if (!res.data.success) { alert(res.data.message); } }; ... <button onClick={() => customFetch()}>API호출버튼</button>no estoy seguro acerca de su estructura de respuesta. El código actual funciona como se esperaba para la estructura:
res = { data: { data: {success: true}}} si no es así, utilice la instrucción if como !res.success
useEffect(() => { setJwt(getClientCookieFromClient('jwt')); }, []); const customFetch = async () => { const res = await axios .get(`${process.env.NEXT_PUBLIC_WECODE_URI}/subscription/master_table`, { headers: { Authentication: jwt, }, }) .then((res) => res.data) .catch((err) => console.log("Error while fetching",err)); //<--- use .catch for catching error if (!res.data.success) { alert(res.data.message); } };Envuelva siempre await dentro del bloque try/catch.
const customFetch = async () => { try { const res = await axios .get(`${process.env.NEXT_PUBLIC_WECODE_URI}/subscription/master_table`, { headers: { Authentication: jwt, }, }) .then((res) => res.data); if (!res.data.success) { alert(res.data.message); } } catch (error) { console.log(error); // Do something with error } };Probar
useEffect(() => { setJwt(getClientCookieFromClient('jwt')); }, []); const customFetch = async () => { const res = await axios.get(`${process.env.NEXT_PUBLIC_WECODE_URI}/subscription/master_table`, { headers: { Authentication: jwt, }, }); if (!res.data.success) { alert(res.data.message); } };