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?