0

I am using Angular for client side and Node and express server for server side and Mongo database.. In my project I want to give some conditions before data to be saved and generate multiple responses as per needed.. I try res.write but give error ..I post my code..In my code I want to send response instead of console.log

  router.post('/addratio',(req,res)=>{



  var requestbody = req.body.items;

  res.setHeader("Content-Type", "text/plain");


  // first, convert data into a Map with reduce
  let counts = requestbody.reduce((prev, curr) => {
    let count = prev.get(curr.Emp_id) || 0;
    prev.set(curr.Emp_id, curr.ValueRatio + count);
    return prev;
  }, new Map());

  // then, map your counts object back to an array
  let reducedObjArr = [...counts].map(([Emp_id,ValueRatio]) => {
    return {Emp_id, ValueRatio}
  });

 /**/


  if(reducedObjArr.length > 0){
     AddRatioPickPoint.find({},(err,found)=>{

           for(var i=0;i<reducedObjArr.length;i++){

             var Empid = reducedObjArr[i].Emp_id;
             var Ratio = reducedObjArr[i].ValueRatio;

             if(Ratio < 100){

             if(found.find(x=>x.Emp_id == Empid)){
               var oldRatio = found.find(y=>y.Emp_id == Empid).ValueRatio;
               console.log(oldRatio);
               if(oldRatio < 100){

                 var addtion = oldRatio + Ratio;
                 //console.log(addtion);

                 if(addtion < 100){
                   AddRatioPickPoint.updateMany({ "Emp_id":Empid },{$set: { "ValueRatio":addtion}},(err,change)=>{
                     if(change){
                       console.log("there is change");
                     }
                   })
                 }

                 if(addtion == 100){

                   AddRatioPickPoint.updateMany({ "Emp_id":Empid },{$set: { "ValueRatio":addtion}},(err,change)=>{
                     if(change){
                       console.log("there is change");
                     }
                   });


                  EmployeeSchema.updateMany({"_id":Empid},{$set: { "status":false}},(err,change)=>{
                   if(change){
                      console.log("there is Emp Status change");
                    }
                  });





                 }





             }
           }



             if(!found.find(x=>x.Emp_id == Empid)){




               AddRatioPickPoint.insertMany({Emp_id:Empid,ValueRatio:Ratio},(err,add)=>{
                 if(add){
                   console.log("New Addition of reqbody success");
                 }
               });




             }



        }

         if(Ratio == 100){

           if(!found.find(t=>t.Emp_id == Empid)){



              EmployeeSchema.updateMany({"_id":Empid},{$set: { "status":false}},(err,change)=>{
               if(change){
                  console.log("there is Emp Status change");
                }
              });

               AddRatioPickPoint.insertMany({Emp_id:Empid,ValueRatio:Ratio},(err,add)=>{
                 if(add){
                   console.log("New Addition of reqbody success");
                 }
               });



             }


              if(found.find(t=>t.Emp_id == Empid)){

              var oldRatio = found.find(t=>t.Emp_id == Empid).ValueRatio;
              console.log(oldRatio);

              console.log("not store");

           }
         }



      }



         for(var i=0;i<requestbody.length;i++){
           var reqEmpID =requestbody[i].Emp_id;
           var reqValueRatio = requestbody[i].ValueRatio;
           var reqClient = requestbody[i].NameClient;
           var reqEmp = requestbody[i].NameEmployee;

           var Alldata = {Emp_id:reqEmpID,NameEmployee:reqEmp,NameClient:reqClient,ValueRatio:reqValueRatio};

           if(requestbody[i].ValueRatio == 100){
             RatioSchema.insertMany(Alldata,(err,add)=>{
               if(add){console.log("Ratio data added");}
             });


           }

            if(requestbody[i].ValueRatio < 100){
             RatioSchema.insertMany(Alldata,(err,add)=>{
               if(add){console.log("Ratio data added");}
             })
           }
         }


     })
   }



});

1 Answers1

0

You need to use res.send().

if(change){

  res.send("there is change");

};

for more information: res.send

hussam
  • 174
  • 1
  • 10
  • I try this..but give error..cannot set headers after they are sent to client..that's why I use res.write but he also give error like cannot write after close – Mansi Agrawal Sep 13 '19 at 12:28
  • I think that's an issue with how you wrote your conditions. You have more than one condition passing at the same time. I suggest that you check the output of console.log to see where that is happening. – hussam Sep 13 '19 at 13:09
  • Thank you all..I did this...all conditions are correctly in work ..I checked in console.log but i want to send response to client which is not done yet.. – Mansi Agrawal Sep 16 '19 at 06:32
  • Can you check this answer, it should be relevant to your situation: https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client. – hussam Sep 16 '19 at 10:58