我想生成多个响应并将其发送到客户端

时间:2019-09-13 10:43:32

标签: express

我正在将Angular用于客户端和Node,并将快速服务器用于服务器端和Mongo数据库。 在我的项目中,我想在保存数据之前给出一些条件,并根据需要生成多个响应。 我尝试res.write但给出错误..我发布我的代码..在我的代码中,我想发送响应而不是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 个答案:

答案 0 :(得分:0)

您需要使用res.send()。

if(change){

  res.send("there is change");

};

有关更多信息:res.send

相关问题