Issue Tracker: Null Response Object

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))  
}) 

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")

EDIT: Disregard - misread.

To set a status code, you can use return res.status(200).json(doc) or somesuch for the status. As your code is right now, if you are getting a completely empty response, I believe the function is executing until the end of the code, missing the response in the conditional at the bottom, and returning without sending what you intend in the response.

I would console.log() every response before you send it from the route (or from every conditional and catch, at least you’ll know what’s executing) or write a quick functional test for the PUT route and log the response that chai gets from your api. Or, as I had to do the other day, comment out all the route code, add a return res.status(200).json({'msg': 'hello'}), try the PUT from a browser and work backward from there.

One thing that may be causing problems is

if (err) console.log(err)

without a return. Often, you need to return a response or call next() or next(error) after catching an error, otherwise, the code may continue to execute. It’s also good practice to use return res.json(doc) for the same reasons to avoid accidentally sending multiple responses.

The good news is if the database is updating, you only have to get the info into the response and send it. Good luck.

1 Like

Thank you for your response! I will try what you have suggested and start the trouble shooting process on Monday. :grin:

Hi @LeiCorre! Did you have any success resolving this issue?

I’ve been stuck on the same problem for a week now.

Hello echoCORN21,

I was able to resolve the issue, in my case I added a then statement onto the end of my findOneAndUpdate() method, and I returned the response object within that then statement… within the PUT request my code turned out something like…

async function myProm() {
  await Project.findOne({project: project}, (err, data) => {
/*logic*/
}) 
}

myProm().then((data, err) => {
  Project.findOneAndUpdate({project: project},  {replacement}, {options}, (err, doc) => {
/*logic*/
 }).then((info, err) => {
if (err) console.log(err);

return res.json(info);
}

}).catch((err)=> {return console.log(err)})

Hopefully this is of some assistance.