【问题标题】:I want to generate and send multiple responses to client side我想生成多个响应并将其发送到客户端
【发布时间】:2019-09-13 10:43:32
【问题描述】:

我在客户端和 Node 上使用 Angular,在服务器端和 Mongo 数据库上使用 express 服务器。 在我的项目中,我想在保存数据之前给出一些条件,并根据需要生成多个响应。 我尝试 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");}
             })
           }
         }


     })
   }



});

【问题讨论】:

    标签: express


    【解决方案1】:

    你需要使用 res.send()。

    if(change){
    
      res.send("there is change");
    
    };
    

    更多信息:res.send

    【讨论】:

    • 我试试这个..但是给出错误..在发送到客户端后无法设置标题..这就是我使用 res.write 的原因,但他也给出了错误,比如关闭后无法写入
    • 我认为这是您如何编写条件的问题。您同时有多个条件通过。我建议您检查 console.log 的输出,看看发生了什么。
    • 谢谢大家......我做到了......所有条件都正常工作......我检查了console.log但我想向尚未完成的客户端发送响应......跨度>
    • 你能检查一下这个答案吗,它应该与你的情况有关:stackoverflow.com/questions/7042340/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多