Metric-Imperial Converter (Quality Assurance) error

On this project, I’m passing all of the main tests, as well as the unit and functional tests (in the console).


Although, I’m getting an error
“Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client” which is preventing me from passing them in the FCC part. The error in the console seems to happen once the functional tests start running.

I’ve done a little research on it, and I semi-understand the error but I just have no idea why it’s happening.

My code is here (in the master branch): GitHub - str8lucK1989/MetricImpConverter
(excuse the messiness and commented out code everywhere, was working through bugs lol)

Here’s it on the web to test it out if you’d like: Metric/Imperial Converter (tranquil-tundra-56846.herokuapp.com)

Challenge: Metric-Imperial Converter

Link to the challenge:

That error occurs when you’ve already sent a response with res.json(), and you still have code afterwards that also tries to send something, like here, when both are invalid:

      if(initNum === "invalid number" && initUnit === "invalid unit") {
        res.json("invalid number and unit");
      }

      if(initNum === "invalid number") {
        res.json("invalid number");
      }

      if(initUnit === "invalid unit") {
        res.json("invalid unit");
      }

      let responseObject = {}
      responseObject['initNum'] = initNum
      responseObject['initUnit'] = initUnit
      responseObject['returnNum'] = returnNum
      responseObject['returnUnit'] = returnUnit
      responseObject['string'] = toString

      res.json(responseObject)

Your problem should be solved if you turn that whole logic into an if-else chain, to make sure you have only one response in all cases.

2 Likes

Awesome. Thanks. That 100% fixed the error and I’ll be sure to keep that in mind for the future. I’m still running into the error of not passing the last 2 challenges despite all my tests passing and having no errors. Upon submitting in the FCC, I’m getting two “undefined” in my console. Did you notice anything else I may have done? Sorry to be a bother and I truly appreciate the help.

I’m not familiar with that project, so I can’t be of much help, sorry.

The undefined comes from these lines in fcctesting.js, it’s initialising a variable error without assigning it to anything, and then logs it. Not sure how that makes sense. The if statement returns false because apparently process.env.NODE_ENV is falsy, and so it goes to
res.json({status: unavailable}):

  let error;
  app.get('/_api/get-tests', cors(), function(req, res, next){
    console.log(error); // <---------------------------------------this is undefined
    if(!error && process.env.NODE_ENV === 'test') return next();
    res.json({status: 'unavailable'});
  },

That’s really all I can say, hopefully someone who is more familiar with the project will chime in.

1 Like

All right man no biggie. I appreciate the help at solving my initial issue! Thank you

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.