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");}
})
}
}
})
}
});