Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "[ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client" errors in earth911 mock APIs #224

Open
vishwaszadte opened this issue Jul 20, 2023 · 1 comment · May be fixed by #242
Labels
back-end back-end related issue good first issue Good for newcomers

Comments

@vishwaszadte
Copy link
Collaborator

vishwaszadte commented Jul 20, 2023

Context

  • We have mock APIs which send a request to the earth911's API and then forward its response to the client
    • /earth911.getPostalData
    • /earth911.searchLocations
    • /earth911.getLocationDetails
  • If you hit any of these API endpoints, with an invalid api_key query parameter, and hit the endpoint again with valid one, you should see the error in the terminal
  • I believe this is because there are missing return statements in the controllers for these endpoints.
  • Example,
router.get("/", async (req, res) => {
  if (req.query.api_key !== "dummykey") {
    res.send({ err: "invalid key" });
  }
  const country = req.query.country;
  const postalCode = req.query.postal_code;

  const API_KEY = process.env.API_KEY;

  try {
    const data = await fetchData(
      `http://api.earth911.com/earth911.getPostalData?api_key=${API_KEY}&country=${country}&postal_code=${postalCode}`
    );

    res.send(data);
  } catch (error) {
    console.log("ERROR: ", error);
  }
});

here, instead of res.send({ err: "invalid key" });, there should be return res.send({ err: "invalid key" });
This will sure that the response is sent and the function call is also over.

@vishwaszadte
Copy link
Collaborator Author

If you find this issue on any other endpoint, feel free to fix that as well

@vishwaszadte vishwaszadte added good first issue Good for newcomers back-end back-end related issue labels Jul 20, 2023
@lugenx lugenx assigned lugenx and unassigned lugenx Jul 20, 2023
@lugenx lugenx added this to ecohabit Aug 7, 2023
@lugenx lugenx moved this to Todo in ecohabit Aug 7, 2023
@rohit-yadavv rohit-yadavv linked a pull request Sep 2, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
back-end back-end related issue good first issue Good for newcomers
Projects
Status: Todo
Development

Successfully merging a pull request may close this issue.

2 participants