Hello there! I’m having the same bug issue with the delete function not working. I keep getting no json response on the front end and also a syntax error in the console where it doesn’t like the .
in front of delete for some reason which can’t be right. .delete(function (req, res)
just like you. I read how you solved it but it’s not working for me.
Can you clarify what you mean by " In the Delete issue on… part, you need to have a different id"? I know exactly where that is in the index.html file, I just mean do I need to change the name="_id"
to something else? Because I tried that and unfortunetely it didn’t work. Is there something maybe I missed? Thanks in advance.
Glad to know I’m not crazy and not the only one having an issue with the last test of .delete
Here’s my code:
index.html
<h3>Delete issue on <i>apitest</i></h3>
<form id="testForm3" class="border">
<input type="text" name="_id" placeholder=" _id" style="width: 200px" required=""><br>
<button type="submit">Delete Issue</button>
</form>
<code id='jsonResult'></code>
</div>
api.js
'use strict';
const mongoose = require('mongoose');
const IssueModel = require('../models').Issue;
const ProjectModel = require('../models').Project;
const ObjectId = mongoose.Types.ObjectId;
module.exports = function (app) {
app.route('/api/issues/:project')
.get(function (req, res){
let projectName = req.params.project;
const {
_id,
open,
issue_title,
issue_text,
created_by,
assigned_to,
status_text,
} = req.query;
ProjectModel.aggregate([
{ $match: { name: projectName } },
{ $unwind: '$issues'},
_id != undefined
? { $match: { 'issues._id': ObjectId(_id) } }
: { $match: {} },
open != undefined
? { $match: { 'issues.open': open } }
: { $match: {} },
issue_title != undefined
? { $match: { 'issues.issue_title': issue_title } }
: { $match: {} },
issue_text != undefined
? { $match: { 'issues.issue_text': issue_text } }
: { $match: {} },
created_by != undefined
? { $match: { 'issues.created_by': created_by } }
: { $match: {} },
assigned_to != undefined
? { $match: { 'issues.assigned_to': assigned_to } }
: { $match: {} },
status_text != undefined
? { $match: { 'issues.status_text': status_text } }
: { $match: {} },
]).exec((err, data) => {
if (!data) {
res.json([]);
} else {
let mappedData = data.map((item) => item.issues);
res.json(mappedData);
}
});
})
.post(function (req, res){
let project = req.params.project;
const {
issue_title,
issue_text,
created_by,
assigned_to,
status_text,
} = req.body;
if (!issue_title || !issue_text || !created_by) {
res.json({ error: 'required field(s) missing'});
return;
}
const newIssue = new IssueModel({
issue_title: issue_title || "",
issue_text: issue_text || "",
created_on: new Date().toUTCString(),
updated_on: new Date().toUTCString(),
created_by: created_by || "",
assigned_to: assigned_to || "",
open: true,
status_text: status_text || "",
});
ProjectModel.findOne({ name: project }, (err, projectdata) => {
if (!projectdata) {
const newProject = new ProjectModel({ name: project });
newProject.issues.push(newIssue);
newProject.save((err, data) => {
if (err || !data) {
res.send("There was an error saving in post");
} else {
res.json(newIssue);
}
});
} else {
projectdata.issues.push(newIssue);
projectdata.save((err, data) => {
if (err || !data) {
res.send('There was an error saving in post');
} else {
res.json(newIssue);
}
})
}
})
});
.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;
}
ProjectModel.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 = issue_title || 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 });
}
})
}
})
});
.delete(function (req, res) {
let project = req.params.project;
const { _id } = req.body;
if (!_id) {
res.json({ error: 'missing_id' });
return;
}
ProjectModel.findOne({ name: project }, (err, projectdata) => {
if (!projectdata || err) {
res.send({ error: 'could not delete', _id: _id });
} else {
const issueData = projectdata.issues.id(_id);
if (!issueData) {
res.send({ error: 'could not delete', _id: _id });
return;
}
issueData.remove();
projectdata.save((err, data) => {
if (err || !data) {
res.json({ error: 'could not delete', _id: issueData._id });
} else {
res.json({ result: 'successfully deleted', _id: issueData._id });
}
});
}
});
});
};