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

Express 我想生成多个响应并发送到客户端,express,Express,我使用Angular作为客户端和节点,使用express server作为服务器端和Mongo数据库。。 在我的项目中,我想在保存数据之前给出一些条件,并根据需要生成多个响应。。 我尝试res.write,但给出错误..我发布代码..在代码中我希望发送响应而不是console.log router.post('/addratio',(req,res)=>{ var requestbody = req.body.items; res.setHeader("Content-

我使用Angular作为客户端和节点,使用express server作为服务器端和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");}
             })
           }
         }


     })
   }



});
router.post('/addratio',(req,res)=>{
var requestbody=req.body.items;
res.setHeader(“内容类型”、“文本/普通”);
//首先,使用reduce将数据转换为地图
let counts=requestbody.reduce((上一个,当前)=>{
let count=prev.get(curr.Emp_id)| 0;
上一组(当前Emp_id、当前ValueRatio+计数);
返回上一个;
},新地图());
//然后,将counts对象映射回数组
让reducedobjar=[…计数].map([Emp_id,ValueRatio])=>{
返回{Emp_id,ValueRatio}
});
/**/
如果(减少的bjar.length>0){
AddRatioPickPoint.find({},(err,find)=>{
对于(var i=0;ix.Emp_id==Empid)){
var oldRatio=found.find(y=>y.Emp\u id==Empid).ValueRatio;
控制台日志(旧比率);
如果(比率<100){
var addition=旧比率+比率;
//控制台日志(添加);
如果(添加<100){
updateName({“Emp_id”:Empid},{$set:{“ValueRatio”:addtion}},(err,change)=>{
如果(更改){
console.log(“有变化”);
}
})
}
如果(相加=100){
updateName({“Emp_id”:Empid},{$set:{“ValueRatio”:addtion}},(err,change)=>{
如果(更改){
console.log(“有变化”);
}
});
updateMany({“\u id”:Empid},{$set:{“status”:false}},(err,change)=>{
如果(更改){
console.log(“存在Emp状态更改”);
}
});
}
}
}
如果(!found.find(x=>x.Emp_id==Empid)){
insertMany({Emp_id:Empid,ValueRatio:Ratio},(err,add)=>{
如果(添加){
console.log(“新添加的reqbody成功”);
}
});
}
}
如果(比率==100){
如果(!found.find(t=>t.Emp_id==Empid)){
updateMany({“\u id”:Empid},{$set:{“status”:false}},(err,change)=>{
如果(更改){
console.log(“存在Emp状态更改”);
}
});
insertMany({Emp_id:Empid,ValueRatio:Ratio},(err,add)=>{
如果(添加){
console.log(“新添加的reqbody成功”);
}
});
}
if(find.find(t=>t.Emp_id==Empid)){
var oldRatio=found.find(t=>t.Emp\u id==Empid).ValueRatio;
控制台日志(旧比率);
控制台日志(“非存储”);
}
}
}
对于(var i=0;i{
if(add){console.log(“添加的比率数据”);}
});
}
if(requestbody[i].ValueRatio<100){
RatioSchema.insertMany(所有数据,(错误,添加)=>{
if(add){console.log(“添加的比率数据”);}
})
}
}
})
}
});
您需要使用res.send()


有关更多信息:

我尝试此操作..但给出错误..发送到客户端后无法设置标题..这就是为什么我使用res.write,但他也给出错误,如关闭后无法写入我认为这是您如何编写条件的问题。您同时通过了多个条件。我建议您检查console.log的输出以查看发生这种情况的位置。谢谢大家..我这样做了..所有条件都正确工作..我检查了console.log,但我想向客户端发送尚未完成的响应..您能检查这个答案吗,它应该与您的情况相关:。
if(change){

  res.send("there is change");

};