0

My page renders details of a selected event from a database of events. It requires two sets of data from two different APIs are present to set-up the page's content.

  • The second api call relies on a piece of information (movieId) from the first call, which it needs to use as an argument
  • So the two API calls need to occur in sequence
  • Importantly, some 'events' have 'externalData' and some don't. Only those with 'externalData' have a 'movieId' and should then be able to call data from the second api

Currently:

  • For simplicity I have stripped down my page so only two h1 tags can be rendered
  • At the moment, only the first h1 tag renders; "{event.name}" (see below)
  • In the serverside console I see that the second api call is either timing out or returning undefined (see location of logs below)
  • I get this error but that is new and seems to be a caching problem in Next.js
  • If I refresh the page, I only get the error

getServersideProps:

export async function getServerSideProps(context) {
        const venueId = context.params.venueId;
        const date = context.params.slug[0];
        const eventId = context.params.slug[1];
        const locale = context.locale;

        const event = await getEventTypeByVenueIdAndDate(venueId, date, eventId);
        const eventAsJson = await JSON.parse(JSON.stringify(event))
        
        let movie

        if (event.externalData) {
            movie = await getMovieById(event.externalData.movieId);
            console.log('ServerSide movie-data:', movie); 
        }

        return {
            props: {
                venueId: venueId,
                movieId: movieId,
                date: date,
                eventProp: eventAsJson,
                eventId: eventId,
                locale: locale,
                movieProp: JSON.parse(JSON.stringify(movie)),
            },
        };
}

Page code:

export default function EventDetailPage(props) {

    const { eventProp, movieId, date, eventId, locale, movieProp, cineamoId } = props

    const [event, setEvent] = useState(eventProp);
    const [movie, setMovie] = useState(movieProp);

    if (!event) {
        return (
            <>
                <LoadingAnimation/>
            </>
        );
    }

    return(<div>
            <h1>{event.name}</h1>
            <h1>{movie.originalTitle}</h1>
    </div>
    )
}

getMovieById function:

import { companyClient } from "./apiClient"

export async function getMovieById(movieId){

    let param = `movie/display/${movieId}`;

    return companyClient.post(param)
        .then(response => {
            console.log(response.data.items[0]);
            return response.data.items[0];
        })
        .catch(function (error) {
            return error;

        });

}

companyClient in apiClient: (these are standard for all apiCalls)

export const companyClient = axios.create({
    baseURL: process.env.NEXT_PUBLIC_ENV_LOCAL_VARIABLE,
    headers: headers,
    data: formData,
    auth: {
        username: "************",
        password: "************"
    },
    timeout: 3000,
    withCredentials: true,
});
Simon Gowing
  • 241
  • 1
  • 2
  • 10
  • I don't see where movieId set for the second call - is it set? If so, what does it log in the if(event.externalData){} condition? – Sean W Aug 31 '21 at 17:57
  • @SeanW Thanks! The movieId issue was a typo I made when simplifying for StackO'. Now corrected above. The log reads "ServerSide movie-data: Error: timeout of 3000ms exceeded at createError (/Users/SimonGowing 1/Documents/cineamo-final/cineamo-nextjs/node_modules/axios/lib/core/createError.js:16:15) at RedirectableRequest.handleRequestTimeout (/Users/SimonGowing 1/Documents/cineamo-final/cineamo-nextjs/node_modules/axios/lib/adapters/http.js:280:16)..." – Simon Gowing Aug 31 '21 at 19:42
  • 1
    Could you share the code for the `getMovieById` function? Also, you should probably check for `event.externalData.movieId` rather than simply `event.externalData` if that's what's used in the API call. – juliomalves Aug 31 '21 at 22:25
  • @juliomalves Thanks again! I just added those details you asked for above: getMovieById function and the corresponding companyClient. You are right about the event.externalData.movieId but because of the way that the data in the second database is built, I can be certain that the movie will certainly have a movieId. The thing that decides whether we should check for one is whether or not it is a movie, which can be defined by whether it has externalData or not. – Simon Gowing Sep 01 '21 at 07:30
  • According to the error you're getting the request is timing out. Are you certain all the options passed to `axios.create` are correct? Can you successfully make the same request (with the exact same options) from Postman (or similar)? – juliomalves Sep 01 '21 at 18:26
  • @juliomalves The axios request works correctly when I hardcode with a movieId and remove the other api call. ie so the getMovieById api call is the only one in the getServerSideProps. So the combination of the two seems to be the problem. Looking into the apiClient in more detail. We identified that in some cases the boundaries in the header and the formData don't match. Still not sure whether this could cause the apiCall to timeOut. Have you ever heard of this happening? – Simon Gowing Sep 02 '21 at 10:31
  • What does `getEventTypeByVenueIdAndDate` look like? Does that request change the `headers`/`formData` in any way that could affect subsequent requests? – juliomalves Sep 02 '21 at 10:36

0 Answers0