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