javascript - Return multiple response from a single function in Node (sails js) -
i developing web application using angular js , sails. struck issue.in application there menu, displays different count values database.plz see back
getallcountmyprofile: function(req,res){ usertokenservice.checktoken(req.get('user-token'), function (err, tokencheck) { var userid = tokencheck.tokendetails.userid; var query = "select (select count(*) review userid="+ userid +" , approvalstatus = 'approved') reviewreceived ,"+ " (select count(*) review reviewerid="+ userid +" ) reviewpenned, "+ "(select count(*) photos userid="+ userid +" , accesstype='private' , status='active') privatephotocount, "+ "(select count(*) photos userid="+ userid +" , accesstype='public' , status='active') publicphotocount"; review.query(query, function (err, photoreviewcount) { if(err){ console.log("error"+ err); } else{ return res.json(200, {status:4, message:'success',data1:photoreviewcount}); } }); userservice.checkvideolimit(userid, function (err, videocount) { var novideos=videocount.data; console.log("video count "+novideos); return res.json(200, {status: 1, data2: videocount}); }); var query="select sum(rating) totalrating,count(*) totalcount review userid=" +userid+ ""; review.query(query, function (err, avgrating) { if(err){ console.log("error"+ err); } else{ var rating=json.stringify(result); var totalrating=result[0].totalrating; var count=result[0].totalcount; var avgratingvalue=(totalrating/count).tofixed(2); console.log(totalrating,count); console.log("avgrating"+rating); return res.json(200, {status:4, message:'success',data3:avgrating}); } }); });` }
if execute each return response got result. if execute above query , got error message on console "can't set headers after sent." need result data1,data2, data3. solution obtain this?
you cannot set response more 1 time. either can use res.write , edit response queries , send res.end() or can change code send response once. this:
res.status(200).json({ "data":{ "data1":photoreviewcount, "data2":videocount, "data3":avgrating } })
Comments
Post a Comment