Issue Tracker: Can't pass certification even though my code works

I am working on the Issue Tracker certification project for QA.
My code passed all of the functional tests but when I the “I’ve completed this challenge” button it fails some tests on the fcc site. After some tests I noticed that when I go to
/api/issues/apitest?open=true on my browser it shows the array with the JSON responses inside. However, if I go to that link inside gitpod, the only response is an empty array, thus fcc thinks it isn’t getting the correct response.
This is my API code:

'use strict';

let issues = [];

module.exports = function (app) {

  app.route('/api/issues/:project')
  
    .get(function (req, res){
      try {
        const project = req.params.project;
        const filters = req.query;

        const filteredIssues = issues.filter(issue => issue.project === project && matchFilters(issue, filters));
        res.json(filteredIssues);
      } catch (err) {
        res.status(500).json({ error: 'Server error' });
      }
    })
    
    .post(function (req, res){
      try {
        const project = req.params.project;
        const { issue_title, issue_text, created_by, assigned_to, status_text } = req.body;

        if (!issue_title || !issue_text || !created_by) {
          return res.status(400).json({ error: 'required field(s) missing' });
        }

        const issue = {
          _id: generateUniqueId(),
          issue_title,
          issue_text,
          created_by,
          assigned_to: assigned_to || '',
          status_text: status_text || '',
          created_on: new Date(),
          updated_on: new Date(),
          open: true,
          project
        };

        issues.push(issue);
        res.json(issue);
      } catch (err) {
        res.status(500).json({ error: 'Server error' });
      }
    })
    
    .put(function (req, res){
      try {
        const project = req.params.project;
        const { _id, ...updates } = req.body;

        if (!_id) {
          return res.status(400).json({ error: 'missing _id' });
        }

        if (Object.keys(updates).length === 0) {
          return res.status(400).json({ error: 'no update field(s) sent', '_id': _id });
        }

        updates.open = (updates.open === 'true')

        updates.updated_on = new Date();

        const issueIndex = issues.findIndex(issue => issue._id === _id && issue.project === project);

        if (issueIndex === -1) {
          return res.status(404).json({ error: 'could not update', '_id': _id });
        }

        issues[issueIndex] = { ...issues[issueIndex], ...updates };

        res.json({ result: 'successfully updated', '_id': _id });
      } catch (err) {
        res.status(500).json({ error: 'Server error' });
      }
    })
    
    .delete(function (req, res){
      try {
        const project = req.params.project;
        const { _id } = req.body;

        if (!_id) {
          return res.status(400).json({ error: 'missing _id' });
        }

        const issueIndex = issues.findIndex(issue => issue._id === _id && issue.project === project);

        if (issueIndex === -1) {
          return res.status(404).json({ error: 'could not delete', '_id': _id });
        }

        issues.splice(issueIndex, 1);

        res.json({ result: 'successfully deleted', '_id': _id });
      } catch (err) {
        res.status(500).json({ error: 'Server error' });
      }
    });
    
};

function matchFilters(issue, filters) {
  for (const key in filters) {
    var boolValue = (filters[key] === 'true')
    if (issue[key] !== boolValue) {
      return false;
    }
  }
  return true;
}

function generateUniqueId() {
  return Date.now().toString(36) + Math.random().toString(36).substring(2, 7);
}

Is this a problem with gitpod? How do I fix it?

Hello and welcome, is there a reason the first line of code is use strict in quotation marks?
note: all these single quotation marks are being changed to double, I dont if this will help, on codepen’s IDE when I analyzed your code.

Don’t send 400 status codes, some of the tests check the response is ok (500 should be fine as that is a server error).

Your filter is returning an empty array. I don’t think matchFilters is doing what you think it is, also remember when there are no filters send req.query is an empty object.

If I fix the filter and remove the 400 statuses, your code passes the tests (running it locally).


I would suggest you use GitHub to share your code in the future.

Thanks for the tips. I didn’t know that by returning 400 the tests would stop working.

It is a little weird, and you are not the first to be bitten by it.

But it makes sense that we check the fetch was successful before testing the response. We can’t know if it is an actual 404 or a manual 404. And we wouldn’t want to test the response from an actual 404. Even if a response arguably should be a “manual” 400 response. That is, the server would not respond with a 400 on its own, but you manually do it because the requested DB resource wasn’t found.

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