【发布时间】:2021-06-24 05:30:31
【问题描述】:
我想调用三个 URL 并从 getServerSideProps() 渲染它们的数据,所以我使用 axios.all([]) 是这样的:
export async function getServerSideProps(ctx) {
const profileURL = 'http://localhost:8000/api/auth/profile/';
const bookmarkURL = 'http://localhost:8000/api/blogs/saved/';
const profileDataReq = axios({method: 'GET',
url: profileURL,
headers: ctx.req ? { cookie: ctx.req.headers.cookie } : undefined
});
const bookmarksReq = axios({method: 'GET',
url: bookmarkURL,
headers: ctx.req ? { cookie: ctx.req.headers.cookie } : undefined
});
const resp = await axios.all([profileDataReq, bookmarksReq]);
const data = axios.spread(function(profile, bookmark) {
console.log('My profile: ', Profile);
console.log('Saved blogs: ', saved);
})
return {
props: { }
};
};
我的问题是如何从getServerSideProps() 的return{...} 返回axios 数据,以便在组件中使用它?
【问题讨论】:
标签: javascript axios next.js