Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Tell us what’s happening:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Your code so far

        /*
*
*
*       Complete the API routing below
*
*
*/

'use strict';
const {v4 : uuidv4} = require('uuid')
function Update(n){
  let filter={$set:{}};
  console.log("update"+JSON.stringify(n))
if(n.issue_title!=""){filter.$set.issue_title=n.issue_title}
if(n.issue_text!=""){filter.$set.issue_text=n.issue_text}
if(n.created_by!=""){filter.$set.created_by=n.created_by}
filter.$set.updated_on=new Date;
if(n.assigned_to!=""){filter.$set.assigned_to=n.assigned_to}
if(n.status_text!=""){filter.$set.status_text=n.status_text}
if(n.open==="false"){filter.$set.open=false}
console.log("filter"+JSON.stringify(filter))
return filter;
}
const checkResult = (want, reference) => {
  // creating a new temp objecct OUTSIDE of the for loop
  let temp = {}
  
  // Array of the reference keys
  const keys = Object.keys(reference)

  // looping over keys in reference
  for (let i = 0; i < keys.length; i++) {
  
    // The current key
    const key = keys[i]
    
    // if the key is included in the 'want' list
    if (key in want) {

      // set a property in the temp object, but with a different value
      temp[key] = 1
    }
  }

  // when we are done, return the object
  console.log(JSON.stringify(temp))
  return temp
}
const Datas = require('../Models/datas');
module.exports = function (app) {

  app.route('/api/issues/:project')
  
    .get(function (req, res){
      console.log(JSON.stringify(req.query))
        if(Object.keys(req.query).length === 0){Datas.find().then((data,err)=>{
           console.log("correct")
          res.json(data)
        })}
        else {
          let find={
             "issue_title": 0,
             "issue_text": 0,
             "created_on":0,
             "updated_on":0,
             "created_by":0,
             "assigned_to":0,
             "open":0,
             "status_text":0,
             "__v":0
          }
          let afterFind=checkResult(find,req.query)
          console.log("got here"+afterFind)
          Datas.find(req.query).select(afterFind).exec()
          .then((data,err)=>{
               if(err){
                 res.send(err)
               }
               res.json(data)
            }
          )
        }
    })
    
    .post(function (req, res){
      let main=req.body;
      console.log(main)
      if(req.body.issue_title==""||req.body.issue_text==""||req.body.created_by==""){
       res.json({ error: 'required field(s) missing' })
      }
   else {
     const id1 = uuidv4()
      let savedata=new Datas({
        "_id":id1,
        "issue_title":req.body.issue_title,
        "issue_text":req.body.issue_text,
        "created_on":new Date(),
        "updated_on":new Date(),
        "created_by":req.body.created_by,
        "assigned_to":req.body.assigned_to,
        "open":true,
        "status_text":req.body.status_text
      })
      savedata.save((err,data)=>{
        if(err){res.json({error:"error has been occurred"})}
        else {
        res.json({
        "_id":id1,
        "issue_title":req.body.issue_title,
        "issue_text":req.body.issue_text,
        "created_on":new Date(),
        "updated_on":new Date(),
        "created_by":req.body.created_by,
        "assigned_to":req.body.assigned_to,
        "open":true,
        "status_text":req.body.status_text
      })};
      })
    }
    })
    
    .put( function (req, res){
    console.log(req.body)
    let obj=req.body

    if(!obj.hasOwnProperty(_id)){
       res.json({ error: 'missing _id' })
    }
    else if(obj.issue_title==""||obj.issue_text==""||obj.created_by==""||obj.assigned_to==""||obj.status_text==""){
      res.json({ error: 'no update field(s) sent', '_id': obj._id })
    }
      Datas.findOneAndUpdate({"_id":req.body._id}, Update(req.body),{new:true}).then((data,err)=>{
        if(err){res.json({ error: 'could not update', '_id': obj._id })}
        console.log(data)
         res.json( { result: 'successfully updated', '_id': data._id })
         })
    })
    
    .delete(function (req, res){
  console.log(req.body)
  let obj=req.body
     if(!obj.hasOwnProperty("_id")){
       res.json({ error: 'missing _id' })
    }
  Datas.findByIdAndRemove(req.body,(err,data)=>{
    if(err){res.json({ error: 'could not delete', '_id': obj._id })}
    res.json({ result: 'successfully deleted', '_id': obj._id })
  })
    });
    
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.

Challenge: Issue Tracker

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).