最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can't return axios.all data from getServerSideProps - Stack Overflow

programmeradmin1浏览0评论

I want to call three URLs and render their data from getServerSideProps(), so I'm using axios.all([]) like this:

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: {   }
    };
};

My question is how do I return the axios data from getServerSideProps()'s return{...} so that I can use it in the ponent?

I want to call three URLs and render their data from getServerSideProps(), so I'm using axios.all([]) like this:

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: {   }
    };
};

My question is how do I return the axios data from getServerSideProps()'s return{...} so that I can use it in the ponent?

Share Improve this question edited Nov 15, 2021 at 23:25 juliomalves 50.7k23 gold badges178 silver badges169 bronze badges asked Mar 27, 2021 at 15:25 forestforest 1,4843 gold badges26 silver badges52 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

I would suggest you use Promise.all instead of axios.all/axios.spread (they've been deprecated) for this use case.

export async function getServerSideProps(ctx) {
    const profileDataReq = axios({
        method: 'GET',
        url: 'http://localhost:8000/api/auth/profile/',
        headers: { cookie: ctx.req.headers.cookie }
    });
    const bookmarksReq = axios({
        method: 'GET',
        url: 'http://localhost:8000/api/blogs/saved/',
        headers: { cookie: ctx.req.headers.cookie }
    });
    const [profile, bookmarks] = await Promise.all([
        profileDataReq,
        bookmarksReq
    ]);

    return {
        props: { 
            profile: profile.data,
            bookmarks: bookmarks.data
        }
    };
};

Also note that ctx.req is always defined in getServerSideProps.

发布评论

评论列表(0)

  1. 暂无评论