Issue Tracker Put Route Trouble

I am on my 5th iternation of this and cant seem to pass the success test. Using the tester on the app as well as checking in the database, everything seems to work.

I even have been following along youtube videos to check my work and i cant seem to see my errors.

.put(function(req, res) {
     let project = req.params.project;
      const {
        _id,
        issue_title,
        issue_text,
        created_by,
        assigned_to,
        status_text,
        open,
      } = req.body;
      if (!_id) {
        res.json({ error: "missing _id" });
        return;
      }
      if (
        !issue_title &&
        !issue_text &&
        !created_by &&
        !assigned_to &&
        !status_text &&
        !open
      ) {
        res.json({ error: "no update field(s) sent", _id: _id });
        return;
      }

      Project.findOne({ name: project }, (err, projectdata) => {
        if (err || !projectdata) {
          res.json({ error: "could not update", _id: _id });
        } else {
          const issueData = projectdata.issues.id(_id);
          if (!issueData) {
            res.json({ error: "could not update", _id: _id });
            return;
          }
          issueData.issue_title = issue_title || issueData.issue_title;
          issueData.issue_text = issue_text || issueData.issue_text;
          issueData.created_by = created_by || issueData.created_by;
          issueData.assigned_to = assigned_to || issueData.assigned_to;
          issueData.status_text = status_text || issueData.status_text;
          issueData.updated_on = new Date();
          issueData.open = open;
          projectdata.save((err, data) => {
            if (err || !data) {
              res.json({ error: "could not update", _id: _id });
            } else {
              res.json({ result: "successfully updated", _id: _id });
            }
          });
        }
      });
    })

I am passing all other tests, except the actual testing tests, so i would assume i am importing everything correct and it is a coding error somewhere

After days and days of banging my head against the wall i discovered it was not matching object ids because i wasnt using the create objectid method from mongoose.

I dont fully understand what it is though.

can a id from mongoose not match to the string of the same info? it needs to be convereted into an object id so that it would match? i thought that i could match the charachters in the object id to a string of the same characters.

is there somewhere that explains how object id works very basic? i cant find anywhere that is letting me understand fully

Every document in a MongoDB collection has a mandatory _id field and is immutable. The _id field can be of any type, a string, number, date an object, or an ObjectId. The field value can be supplied by the user, else MongoDB creates the value of type ObjectId.

When you query the database using a filter, the document field and the provided variable types need to be the same. For example, you can compare a string and number as in JavaScript (this is true in JS: '12' == 12, but not in a MongoDB query). So, when you are comparing a field of type ObjectId, the supplied value need to be the same type.

In your web app, the value extracted from the request data is of type string. So, when you want to match the id values your code need to convert the string the appropriate type. With Mongoose ODM, use the mongoose.Types.ObjectId().

1 Like

thank you. you provided what was missing from my understanding.