4

I have a reddit like application. The node/express TypeORM server with postgres db is on Heroku. The client with Next.js is on Vercel. Registration, comments, upvote/downvote are working fine. When I try to create a new community I get the following error:

502: BAD_GATEWAY Code: NO_RESPONSE_FROM_FUNCTION ID: cdg1::lrlvg-1612986052014-c0b0a2cd09ac

In Vercel function logs or on PC in terminal I get an error:

Error: Your getServerSideProps function did not return an object. Did you forget to add a return?

The code:

export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
  try {
    const cookie = req.headers.cookie
    if (!cookie) throw new Error('Missing auth token cookie')

    await Axios.get('/auth/me', { headers: { cookie } })

    return { props: {} }
  } catch (err) {
    res.writeHead(307, { Location: '/login' }).end()
  }
  
}

When I run the client and the server on PC it works, but when deployed I get the error posted above.

juliomalves
  • 42,130
  • 20
  • 150
  • 146
Vilmos Szabó
  • 167
  • 1
  • 2
  • 10

2 Answers2

9

From Next.js 10 getServerSideProps supports returning a redirect object for this exact purpose - which also solves the error you're getting by explicitly returning an object.

export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
    try {
        const cookie = req.headers.cookie
        
        if (!cookie) throw new Error('Missing auth token cookie')

        await Axios.get('/auth/me', { headers: { cookie } })

        return { props: {} }
    } catch (err) {
        // Handle error

        return {
            redirect: {
                destination: '/login',
                statusCode: 307
            }
        }
    }
}
juliomalves
  • 42,130
  • 20
  • 150
  • 146
0

The catch doesn't return. You can do

res.writeHead(307, { Location: '/login' }).end()
return { props: {ok: false, reason: "some error description for your own consumption, not for client side"}

Similarly, inside the try block, maybe you also want to res.writeHead(200) and push some data to the server?

I don't know very well why, on success, you return the data as a function return value, and on failure, you send the error through HTTP. I think this might be your point of failure, and that both sides should either return HTTP, or a function return value, or both, but not one on one and another one on the other.

Besto
  • 388
  • 1
  • 13
  • When I run the client and the server on my PC it's working so I think this error is because of heroku. I have no clue tbh. – Vilmos Szabó Feb 11 '21 at 11:16
  • Some things you can look out for are: often, such environments have different write permissions, so things that work on computers often fail on remote environments. Another things is whether the environment on your computer is the same as the remote one: do you have the same path `/login` and `/auth/me` listeners? Maybe on your computer, there are no errors, so it doesn't go to the catch function, but on heroku it does, and since the catch doesn't have a `return` value, it works in one and fails silently in the other. – Besto Feb 11 '21 at 16:35
  • The catch with return gives back this error: [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client – Vilmos Szabó Feb 11 '21 at 17:35