Hello Friends,
I’ve been working on the Issue Tracker Project and I am currently in the process of writing the routes. I haven’t had any issues with my POST request, but I notice that my PUT request returns an empty response object even though it does update the database.
If I console log the entire response object, it shows that it has a body and such, but fields like “StatusCode” will say null. If I try to access any part of the actual response object it will return as “undefined”.
From what I’ve been reading in other places, this issue generally occurs when there is an issue with either the HTTP headers or if the response object is being returned too quickly.
This is my second time trying this project and getting stuck at the same place… last time I tried many suggestions in regard to how to fix this issue, if it had arose out a problem with the HTTP headers. But I had no success. Which leaves me thinking the issue may be that the response object is being returned too quickly.
I have been dabbling with async/await and Promises as a potential fix but I am newer to both these parts of JavaScript. Perhaps the issue lies else wear in my code, I am open to any suggestions & I would be be extremely grateful for any assistance.
This is the link to my Issue Tracker The Issue Tracker
And the code for my PUT request route
.put(function (req, res){
let project = req.params.project;
let { _id, issue_title, issue_text, created_by, assigned_to, status_text, closed } = req.body
let update = {
issue_title: issue_title,
issue_text: issue_text,
created_by: created_by,
assigned_to: assigned_to,
status_text: status_text,
updated_on: new Date(),
closed: false
}
let replacer;
let numero = 0
async function myProm() {
await Project.findOne({project: project}, (err, data) => {
for (let i=0; i < data.issues.length; i++ ) {
data.issues[i].index = numero++
}
let result = data.issues.filter(i => JSON.stringify(i._id) == JSON.stringify(_id))
if (issue_title) {
result[0].issue_title = update.issue_title
}
if (issue_text) {
result[0].issue_text = update.issue_text
}
if (created_by) {
result[0].created_by = update.created_by
}
if (assigned_to) {
result[0].assigned_to = update.assigned_to
}
if (status_text) {
result[0].status_text = update.status_text
}
if (closed) {
result[0].closed = true
}
result[0].updated_on = new Date()
data.issues.splice(result[0].index, 1, result[0])
if (data) console.log('no. 1')
if (err) console.log(err)
replacer = data.issues
})
}
myProm().then((data, err) => {
Project.findOneAndUpdate({project: project}, {project: project, issues: replacer}, {returnOriginal: false, useFindAndModify: false}, (err, doc) => {
if (err) console.log(err)
if (doc) {
console.log("no. 2")
res.json(doc)
}
})
}).catch((err)=> console.log(err))
})