fetching data form strapi with bearer token #2451
-
hi there im using strapi for cms and next js for frontend, and using swr and axios for data fething
and when i console.log(data) it's always shown undefined , it's something wrong with my code ? it's any correct way to fetch data with bearer token? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
@garudaputro but why you pass const address = `${process.env.NEXT_PUBLIC_API_URL}/products`
const auth = `${process.env.NEXT_PUBLIC_API_TOKEN}`
async function fetcher(address) {
await axios
.get(url, {
headers: {
Authorization: `Bearer ${auth}`,
},
})
.then((res) => res.data);
}
const {data, error} = useSWR(address, fetcher) |
Beta Was this translation helpful? Give feedback.
-
yeah there was a typo. Data are loaded async so don't expect it to be there just after hook call. Show me whole component, and how you render data. Is there anything in |
Beta Was this translation helpful? Give feedback.
yeah there was a typo. Data are loaded async so don't expect it to be there just after hook call. Show me whole component, and how you render data. Is there anything in
error?
how aboutisLoading