Hello,
Im working on Issue Tracker project. My console showed the update is successfully, but actually did not update on mongo atlas database.
this is the doc i am trying to change the value of ‘issue_text’ from ‘before update’ to ‘after update’:
console showed the doc is updated successfully(issue_text is updated):
but when i check my mongo atlas database, the ‘issue_text’ didn’t update:
here is my code:
.put(async function (req, res) {
// get the params from url.
let project = req.params.project;
const {
_id,
issue_title,
issue_text,
created_by,
assigned_to,
status_text,
open,
} = req.body;
const update = {};
// using loop to get each update info from req.body
for (const key of Object.keys(req.body)) {
if (req.body[key] !== "") {
update[key] = req.body[key];
}
}
try {
ProjectModel.findOne({ name: project }, async (err, projectdata) => {
if (err || !projectdata) {
res.json({ error: "could not update", _id: _id });
} else {
console.log("we find the project");
console.log(projectdata);
projectdata.issues.forEach((issue) => {
console.log(issue._id);
if (issue._id == _id) {
console.log("we find the issue");
console.log(issue);
issue.issue_title = issue_title || issue.issue_title;
issue.issue_text = issue_text || issue.issue_text;
issue.created_by = created_by || issue.created_by;
issue.assigned_to = assigned_to || issue.assigned_to;
issue.status_text = status_text || issue.status_text;
issue.open = open;
issue.updated_on = new Date();
projectdata.save((err, data) => {
if (err || !data) {
res.json({ error: "could not update", _id: _id });
} else {
console.log("update is successfully");
console.log(data);
res.json({ result: "successfully updated", _id: _id });
}
});
}
});
}
});
} catch (err) {
console.log(err);
}
})
I saw many people using projectdata.issues.id(_id)
to get the issue by ID. when i used this code, i got a TypeError: projectdata.issues.id is not a function
. Instead, i used forEach
to get the issue by id.