Issue Tracker Delete and PUT Issue - Need Help

Hi guys, I am working on Issue Tracker of Quality Assurance Projects. Could not pass below requirements:

When the PUT request sent to /api/issues/{projectname} does not include update fields, the return value is { error: ‘no update field(s) sent’, ‘_id’: _id }. On any other error, the return value is { error: ‘could not update’, ‘_id’: _id }.

You can send a DELETE request to /api/issues/{projectname} with an _id to delete an issue. If no _id is sent, the return value is { error: ‘missing _id’ }. On success, the return value is { result: ‘successfully deleted’, ‘_id’: _id }. On failure, the return value is { error: ‘could not delete’, ‘_id’: _id }.

My code:

.put(function (req, res) {
let project = req.params.project;
let body = req.body;
let _id = req.body._id;

  if (_id == "" || !_id) {
    return res.json({ error: 'missing _id' })
  }

  function clean(myObj) {
    Object.keys(myObj).forEach((key) => (myObj[key] == '') && delete myObj[key]);
    return myObj
  }
  console.log(body)

  let updateBody = clean(body);
  delete updateBody['_id'];
  console.log(body)

  console.log(updateBody)

  if (Object.keys(updateBody).length < 1) {
    console.log('no update field(s) sent')
    return res.json({ error: 'no update field(s) sent', _id })
  }

  myDb.updateIssue(project, _id, updateBody, (err, data) => {
    if (err) {
      return res.json({ error: 'could not update', _id })
    }
    /* res.json({
      _id: data._id, issue_title: data.issue_title, issue_text: data.issue_text, created_on: data.created_on.toISOString(), updated_on: data.updated_on.toISOString(),
      created_by: data.created_by, assigned_to: data.assigned_to, open: data.open, status_text: data.status_text
    }) */
    console.log('successfully updated')
    res.json({ result: 'successfully updated', _id })
  });

})

.delete(function (req, res) {
  let project = req.params.project;
  let _id = req.body._id;

  if (_id == "" || !_id) {
    return res.json({ error: 'missing _id' })
  }

  myDb.deleteIssue(project, _id, (err, data) => {
    if (err) {
      console.log(err)
      return res.json({ error: 'could not delete', _id })
    }
    res.json({ result: "successfully deleted", _id })
  })

});`

My DB Code:

const updateIssue = (project, _id, updateBody, done) => {

let Issue = mongoose.model('Issue', issueSchema, project);

updateBody.updated_on = new Date();

Issue.findByIdAndUpdate(_id, updateBody, { new: true }, (err, data) => {

    done(err, data);

});

};

const deleteIssue = (project, _id, done) => {

let Issue = mongoose.model('Issue', issueSchema, project);

Issue.findByIdAndDelete(_id, (err, data) => {

    done(err, data);

});

}

Any idea why this is not working?

Github link: GitHub - enszrlu/Issue-Tracker: FCC Quality Assurance Projects - Issue Tracker
Heroku Link: https://quiet-fjord-29342.herokuapp.com/

Finally found the solution:

Changed this:

myDb.deleteIssue(project, _id, (err, data) => {
if (err) {
return res.json({ error: ‘could not delete’, _id })
}
res.json({ result: “successfully deleted”, _id })
})
to this on if section:
myDb.deleteIssue(project, _id, (err, data) => {
if (!data || err) {
return res.json({ error: ‘could not delete’, _id })
}
res.json({ result: “successfully deleted”, _id })
})

Credits: Issue Tracker Project - Fails FCC delete request test but works for me - #2 by Sky020

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