score:0
You are not using the jwtToken
for anything after passing it to the service.
Before the line const instance = axios.create(options);
add something like this:
if (jwtToken !== null) {
options.headers.Authorization = `Bearer ${jwtToken}`
}
score:0
In my NextJS begining I followed this tutorial , and I changed fetch to axios in this way:
export const getStaticPaths = async () => {
const res = await fetch('https://jsonplaceholder.typicode.com/users');
const data = await res.json();
const paths = data.map((ninja) => {
return {
params: { id: ninja.id.toString() },
};
});
return {
paths,
fallback: false,
};
};
export const getStaticProps = async (context) => {
const id = context.params.id;
const res = await fetch(`https://jsonplaceholder.typicode.com/users/${id}`);
const data = await res.json();
return {
props: { ninja: data },
};
};
I applied the change using useEffect()
useEffect(() => {
// const data = await fetch(`https://jsonplaceholder.typicode.com/users/${id}`);
// const res = await data.json();
// setninja(res);
const fetchData = async () => {
const result = await axios(`https://jsonplaceholder.typicode.com/users/${id}`);
setninja(result.data);
};
fetchData();
console.log(data);
}, []);
I hope this info will be useful for you.
score:1
Your problem is that your async method does not return a promise.
import service from '../service'
export const getAllAccounts = async (adminToken) => {
const res = service({ jwtToken : adminToken }).get(`/accounts`);
return res;
}
score:3
Do not use Axios. Just use fetch()
.
Next.js polyfills fetch()
by default on both the client and server, so you can just use it:
In addition to
fetch()
on the client-side, Next.js polyfillsfetch()
in the Node.js environment. You can usefetch()
in your server code (such asgetStaticProps
/getServerSideProps
) without using polyfills such asisomorphic-unfetch
ornode-fetch
.
Source: stackoverflow.com
Related Query
- How to fetch API data from Axios inside the getServerSideProps function in NextJS?
- how to properly replace axios api with fetch api and map over the received data in nodeJS?
- How to fetch api from the function which pushes each api in array?
- How to fetch json data from a url that requires an api key to access the data in reactjs?
- Fetch random data from API without refreshing the page in react axios
- How to fetch data from Nextjs API route when build and deploy?
- how fetch data from database through api using axios in reactjs?
- how to fetch data with nextjs from backend API end point in production
- How to pass data from a parent component or use axios here in the map function for react-google-maps library?
- how to fetch the data from server like given in console in reactjs using axios
- How can I fetch data from an array of objects, pass it through an API end point to .fetch() the needed object value using React?
- How to put Graphql queries to fetch data from Wordpress API inside react components instead of templates in GatsbyJS?
- How to properly return value from a data inside the function
- How to fetch using axios when the api comes from a state via reducer?
- How to handle large sets of data response from an api fetch when no size/limit/count parameters are present for the api?
- How to fetch data from Instagram api with Axios and Reactjs?
- How to fetch only the first data from an API in React?
- how can i send an api call from the supreme function to another component called product to change a mock data called cart: true and display in cart
- How to get data from the route url to put inside the callback function in React router?
- How to properly fetch data from several API calls and display it to the DOM in React
- How to put a dynamic data from firestore in the function where() and also use the snap.size to count the total query to be passed in a graph?
- React Hook useEffect : fetch data using axios with async await .api calling continuous the same api
- How to fetch data in getInitialProps with redux-saga.Then get the response from redux store while in the getInitialProps method?
- How to generate a unique id automatically for the data that is posted to an API server using axios library in redux
- How to make the Service Worker cache data from API and update the cache when needed
- How to fetch data from api and pass it as props
- How to use the global data from React inside Apollo client's initialization?
- How to get value from useState inside the function
- How to retrieve data from the server using fetch get method and show it in a table
- How to make React.js fetch data from api as state and pass this state data from its parent to child component
More Query from same tag
- Where can I find the API guide for this usage of passing props to children in React
- React data export to excel (xls)
- ReactJs MaterialUi onRowUpdate field validation
- Javascript code gets "Attempted import error:" in react-scripts build when importing from Typescript file
- Use React Toast Notification in class component
- Error: Cannot convert undefined or null to object
- How to combine JSX component with dangerouslySetInnerHTML
- Is it possible to write two condition in one curly braces?
- How to create login configuration
- How to semantically mark up multilevel checkbox and radio groups?
- onClick inline function works different from outside function
- Blinking Modal with Semantic UI React
- Simple react component - Expected an assignment or function call and instead saw an expression no-unused-expressions
- JSON.stringify() function is replacing the file object with an empty one
- I am trying to remove cookies in reactjs but it does not get removed
- add link to object array
- React input binding to state vs local variable
- How can i push array in matrix vertically?
- What are some GraphQL schema naming best practices?
- useEffect and dependency array
- React.StrictMode: SetState function in useEffect is run multiple times when effect is run once
- How do I configure jsdom with jest
- Using RxJs takeUntil() function with apollo-client
- change text of a specific button when clicked in React
- Getting react-widgets' elements to align in row-wise flexbox
- React how can i redirect the homepage?
- How to upgrade from tailwindcss 2 to 3 and create-react-app 4.0.3 to 5.0.0 (due to Post CSS 8 )
- React Redux not updating state despite login working fine
- How to render dynamic components onClick
- How to show a div on input focus with CSS?