Possible broken test - Quality Assurance Project 2 - Issue Tracker

Hi Campers,

After spending the best part of 12 hours trying to debug this test I’m either missing something really silly or the test might be broken - I’ve tried probably 50 different ways of getting the code to error in the way the test wants it to but without visual on the test scripts I cant be sure.

The user story is:

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 }.

and here is my .put

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

          //console.log(req.body) 

          let project = await Project.findOne({
            project_name: req.params.project
          }, async (err, project) => {
            if (err) {
              console.log('general error')
              res.json({
                error: 'could not update',
                '_id': req.body._id
              });

            }


            if (req.body.issue_title == '' && req.body.issue_text == '' && req.body.status_text == '' && req.body.created_by == '' && req.body.assigned_to == '') {
              //  console.log('missing fields error')
              return res.json({
                error: 'no update field(s) sent',
                '_id': req.body._id
              })
            }

            if (!req.body) {
              console.log("no body")
              res.json({
                error: 'could not update',
                '_id': req.body._id
              })
            }

            if (!req.body._id) {
              //  console.log("error NO ID")
              return res.json({
                error: 'missing _id'
              })
            }


            project.issues.forEach(issue => {
              //console.log('scanning: ' + issue._id + ' body id: ' + req.body._id)
              if (issue._id == req.body._id) {
                //console.log('found record')



                if (req.body.issue_title) {
                  //    console.log('setting issue title')
                  issue.issue_title = req.body.issue_title
                }
                if (req.body.issue_text) {
                  issue.issue_text = req.body.issue_text
                }
                if (req.body.status_text) {
                  issue.status_text = req.body.status_text
                }
                if (req.body.created_by) {
                  issue.created_by = req.body.created_by
                }
                if (req.body.assigned_to) {
                  issue.assigned_to = req.body.assigned_to
                }
                if (req.body.open == 'false') {
                  issue.open = false
                }

                issue.updated_on = new Date()



              }
            })



            await project.save((err, data) => {

              console.log(data._id || 'data no found')

              if (err) {
                console.log('err in save')
                return res.json({
                  error: 'could not update',
                  '_id': req.body._id
                })
              }

              return res.json({
                result: "successfully updated",
                _id: req.body._id
              })

            })




          })



        })

The actual put update works fine as verified from my mongoDB, and if I include a valid ID and no update fields it errors in exactly the way requested, if I enter an incorrect ID it errors in the way requested and the general catch errors are all formatted in the requested way.

Any help would be super appreciated

Did you ever figure this out? I am having the same exact problem. Every time I test the fields it seems to give the correct error response but I still fail the test as well.

Hi Lace,

Only through messing around and trying different things - here’s the put that eventually worked:

    .put(async (req, res)=>{
      let project = req.params.project;
      let fields = ['_id','issue_title', 'issue_text', 'created_by', 'assigned_to', 'status_text'];
      let fieldsToUpdate= queryOrBodyParser(req.body,fields);

      if(Object.keys(fieldsToUpdate).length<2){
          if(!req.body._id){
            return res.send({error:'missing _id'})
          }
          return res.send({error: 'no update field(s) sent', _id:fieldsToUpdate._id})
      }

      let issue=await Issue.findOne({_id:fieldsToUpdate._id}).exec();

      if(!issue){
        return res.send({error:'could not update', _id:fieldsToUpdate._id});
      }

      delete fieldsToUpdate._id;
      Object.keys(fieldsToUpdate).forEach(key=>{
       issue[key]=fieldsToUpdate[key];
       issue.markModified('key');
      })
      issue.updated_on=Date();

      issue.save(function (err){
        if(err) return handleError(err);
      });

      res.json({result: 'successfully updated', _id:issue._id})
    })

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

2 Likes

I figured out my problem, I wasn’t taking into consideration that the _id was automatically put in to the updatedObject. So, Object.keys(updatedObject) had two keys in there with date and _id. Not just the date. That helped me fix my else if logic and therefore pass the tests. I finally figured this out when making the tests with chai. It was cool to see another use for these tests as without writing the functional tests I would not have figured that out last night.

never mind. Still fail the test lol

1 Like

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