Failing on PUT request in Quality Assurance Project

Tell us what’s happening:
I’m failing this test for a week and I can’t figure out what’s wrong with my code :scream:

You can send a PUT request to /api/issues/{projectname} with an _id and one or more fields to update. On success, the updated_on field should be updated, and returned should be { result: 'successfully updated', '_id': _id } .

I’m updating the updated_on and returning the { result: 'successfully updated', '_id': _id } when the update is successful and still I’m not passing this test.

Here’s my code on PUT endpoint:

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

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

      console.log(issueId, req.body, Object.keys(req.body).length);

      if(req.body.constructor === Object) {
        let bodyKeys = Object.keys(req.body);
        if(bodyKeys.length <= 1) { // "<=1" because id is counting as a field
          console.log("no updated field(s) sent", issueId);
          return res.json({
            error: 'no update field(s) sent',
            '_id': issueId 
          });
        } else if(bodyKeys.length > 1) {
          let hasUpdatedFields = false;
          bodyKeys.forEach(key => {
            if(key != "_id" && req.body[key]) {
              hasUpdatedFields = true;
            }
          });
          if(!hasUpdatedFields) {
            console.log("no update field(s) sent", issueId);
            return res.json({
              error: 'no update field(s) sent',
              '_id': issueId
            }); 
          }
        }
      }

      console.log(project, issueId);

      Project.findOne({project_name: project}, function(err, doc) {
        if(err) {
          res.send(`Error: ${err}`);
        } else if(doc) {
          let issue = doc.project_issues.id(issueId);
          // If user sends an ID, but it doesn't exist in that project
          if(!issue) {
            console.log("issue doesn't exist on project", issueId);
            return res.json({
                error: 'could not update',
                '_id': issueId 
              });
          }

          console.log('issue before');
          console.log(issue);
          
          let updatedData = {
            issue_title: req.body.issue_title,
            issue_text: req.body.issue_text,
            created_by: req.body.created_by,
            assigned_to: req.body.assigned_to,
            status_text: req.body.status_text,
            updated_on: new Date(),
            open: req.body.open
          };
          
          for(const key in updatedData) {
            if(updatedData[key]) {
              console.log(key, issue[key], updatedData[key]);
              issue[key] = updatedData[key];  
            }
          }
          console.log('issue after');
          console.log(issue);

          /* Save updates on mongoDB */
          doc.save(function(err) {
            if(err) {
              console.log("err to save:", issueId, err);
              return res.json({
                error: 'could not update',
                '_id': issueId 
              });
            } else {
              console.log("success", issueId);
              return res.json({
                result: 'successfully updated',
                '_id': issueId
              });
            }
          });
        }
      });
   })

Can anyone help me see what I’m doing wrong here? :pray:
Thank you!

Your project link(s)
solution: https://replit.com/@rodrigosuguimot/boilerplate-project-issuetracker2

Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36

Challenge: Issue Tracker

Link to the challenge:

Log all your PUT inputs and outputs. When you do, look for the issue with the “Put Target” text and note the id of the issue. Then visit your-project-url/api/issues/fcc-project?_id={issueId} and you should get an array containing the one issue, but it’s empty. That’s how the project test checks that updated_on was updated.

So, your filtering logic in the GET route needs checking. These projects are complicated and a problem in one route can cause problems in other route’s tests.

1 Like

Hi, Jeremy!
It worked!!! :raised_hands:

Thank you very much!