1

I have an endpoint setup that queries an external database and page that calls that endpoint inside of getServerSideProps. With the following code, When I visit the API endpoint the data is successfully returned but when I go to the page I I get this error: API resolved without sending a response for /api/get_bans, this may result in stalled requests.

export const getServerSideProps = async () => {
  let bans = await fetch('http://localhost:3000/api/get_bans').then((res) => { 
    return res.json();
  })

  return {
    props: { bans: bans }
  }

}

API Endpoint

import db from '../../db.js';

const handler = (req, res) => {
  if(req.method == 'GET') {
    db.connect();
    db.query('SELECT * FROM tf2jr_guardbans_logs', (error, results, fields) => {
      if (error) throw error;
      res.status(200).json(results);
    })
  }
}

export default handler;
7
  • Does this answer your question? API resolved without sending a response in Nextjs Commented Jun 18, 2021 at 22:20
  • If you're having a problem with the request handler for /api/get_bans, then please show us all the code for that request handler. Commented Jun 18, 2021 at 23:00
  • Also, if (error) throw error; should NEVER be inside a plain asynchronous callback. Log the error and send an actual error response such as console.log(err); res.sendStatus(500) when you get that error. Throwing there will do you no good at all. Commented Jun 18, 2021 at 23:00
  • @jfriend00 what are you even saying in your last comment? It waits for the fetch to finish, then in the callback function it returns the response then getServerSideProps returns that Commented Jun 19, 2021 at 0:22
  • @JordanBaron - OK, I was confused by res.json() which is the same syntax that is used when sending a JSON response to an http request. I thought you were sending an http response. So, that comment will be deleted. Commented Jun 19, 2021 at 0:34

1 Answer 1

1

if you replace

if (error) throw error;

with

if (error) return res.status(500).json(error.message);

you should see the actual error

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.