1

![enter image description here][1]

I'm using sailsJs in my application. My server runs properly in my local, but server throws error "process.nextTick() Error : Can't set header after they sent". Local version is working fine, But server throws above error. And I could not use any setInterval function in application. Don't know why this has happened.

My Controller Code

       var lodash = require("lodash");

var FileController = {
    upload: function (req, res) {

if(req.method === 'POST'){
console.log('plus button pressed........');
var filenameOriginal;
var carRecordFound=false;
req.file('uploadFile').upload({saveAs: function(file, cb) {
    cb(null, file.filename);
    filenameOriginal=file.filename;
  },
  dirname: '../../assets/images'
}, function whenDone(err, uploadedFiles) {
    if(req.param('plateNumber')!=undefined){
    var originalPlateNumber=req.param('plateNumber');
    var updatedPlateNumber=originalPlateNumber.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();

          if(err){
            return res.json(500, err);
          }
          else if(uploadedFiles.length === 0){

Account.findOne(req.param('accountID')).populateAll().exec(function found(err, accountData){ 
if (err) return next(err);
if (!accountData) {res.send({notValid : 'notValid'}); return;}

var i=0;
validate(i);
function validate(i){                                               
if(i < accountData.subscriptionLog.length){
//
if(accountData.subscriptionLog[i].subscriptionStatus==undefined){ //SUBSCRIPTION STATUS UNDEFINED STGRTS HERE                                                                           
console.log(i+'--Subscription is undefined');
i++;
validate(i);
}//SUBSCRIPTION STATUS UNDEFINED ENDS HERE
else if(accountData.subscriptionLog[i].subscriptionStatus=='active'){ //SUBSCRIPTION STATUS ACTIVE STARTS HERE
if(currentDatems<=(accountData.subscriptionLog[i].subscriptionEndDate).getTime()){ //SUBSCRIPTION END DATE VALIDATION STARTS HERE
    console.log(i+'--<<uniqueID-------'+accountData.subscriptionLog[i].uniqueID+'Max'+accountData.subscriptionLog.length);
    uniqueID=accountData.subscriptionLog[i].uniqueID;   
    console.log(i+'--<<Strated Processing-------'+accountData.subscriptionLog[i].uniqueID);                                                                 
    Mastertransactional.count({uniqueID : uniqueID}).exec(function countCB(err, found){ //RESPONSE FROM SERVER FOR THE SUBSCRIPTION COUNT
        if (err) { console.log('Error------'+err); exitBoolean=true; return next(err);  };
        if (!found || found==0) {
            console.log('No cars parked for this subscription!!!'+found);
        }
        console.log('There are '+ found +'  records founded ');
        found1=found;       
                console.log("numberOfCars-------"+accountData.subscriptionLog[i].numberOfCars);
                console.log("found cars-----------"+found1);
           if(found1<(accountData.subscriptionLog[i].numberOfCars)){//CAR COUNT VALIDATION STARTS HERE
                activeSubscriptionFound=true;
                console.log(new Date()+"active condition--------"+activeSubscriptionFound);
                console.log('entered into less than car');
                if(req.param('parkingID')==undefined || req.param('parkingID')=='' || req.param('plateNumber')=='' || req.param('plateNumber')==undefined )// check empty object from plus button
                {
                    console.log('plus button');
                    exitBoolean=true;
                }
                else if(req.param('parkingID')!=undefined || req.param('parkingID')!='')
                {
                    console.log('Car stored');
                    // ADD THE CAR IN THE DAILY TRANSACTION     
                        Dailytransactional.create(carObj).exec(function(error,carObj){
                                if(error){console.log('error');}
                                console.log("daily"+carObj);
                                Dailytransactional.publishCreate({id: carObj.id,
                                 parkingID: carObj.parkingID,
                                 plateNumber: carObj.plateNumber,
                                 snap: carObj.snap,
                                 parkingZone: carObj.parkingZone,
                                 color: carObj.color,
                                 brand: carObj.brand,
                                 employeeID: carObj.employeeID,
                                 accountID: carObj.accountID,   
                                 venue: carObj.venue,
                                 status:carObj.status,
                                 log:carObj.log
                                });
                        carObj1['transactionID']=carObj.id;
                        carObj1['uniqueID']=uniqueID;
                        Mastertransactional.create(carObj1).exec(function(error,carObj1){
                            if(error){console.log('error');}
                            console.log("Master created ---"+carObj1);
                            exitBoolean=true;

                        }); 
                    });     
                    if((found1+1)==accountData.subscriptionLog[i].numberOfCars){
                        console.log('Maximum car reached for the subscription');
                        //CHANGE STATUS OF THE SUBSCRIPTION TO EXPIRED
                        FileController.subscriptionExpire(req.param('accountID'), accountData.subscriptionLog, i);

                    }// found +1 if close
                }// else if for record insert
                console.log('Car Count True Loop Count Incremented');
                if(!exitBoolean)
                {
                    i++;                                                                                
                    validate(i);                                                                                
                }                                                                               
           }//CAR COUNT VALIDATION ELSE LOOP HERE
           else {

                FileController.subscriptionExpire(req.param('accountID'), accountData.subscriptionLog, i);
                console.log('Car Count Else Loop');
            }//CAR COUNT VALIDATION ENDS HERE
            console.log(exitBoolean+'After Else Loop >> Counter INcreased'+activeSubscriptionFound);
            if(!exitBoolean)
            {
                i++;
                validate(i);
            }
            if(exitBoolean)
            {
                if(!activeSubscriptionFound){
                    //show error message that no active subscription available
                    console.log(found1+'plus button error called...'+new Date());   
                    res.send({success : 'error'});
                    return;
                }
                else
                {
                    console.log(found1+'plus button success'+new Date());   
                    res.send({success : 'success'});
                    return;
                }
             }
             else
             {
                console.log(new Date()+'This is the error'+exitBoolean);
             }
    });// MASTER DATA COUNT FUNCTION ENDS HERE                                  
    }// SUBSCRIPTION END DATE VALIDATION ELSE LOOP
else {
FileController.subscriptionExpire(req.param('accountID') ,accountData.subscriptionLog, i);

    if(!exitBoolean)
    {
        i++;
        validate(i);
    }
}   //SUBSCRIPTION END DATE VALIDATION ENDS LOOP                                                    
} // SUBSCRIPTION IN LOOP IS NOT ACTIVE                                                 
else
{
i++;
console.log(i+'Expired Subscription');
validate(i);
}//SUBSCRIPTION LOOP ENDS HERE
}//MAIN ENTRANCE ENDS HERE
else
{
exitBoolean=true;

if(!activeSubscriptionFound){
console.log(found1+'plus button error'+new Date()); 
res.send({success : 'error'});

}
}
console.log('coming here<< active subscription--'+activeSubscriptionFound+"--------exitBoolean <<"+exitBoolean);

if(exitBoolean)
{
if(!activeSubscriptionFound){
//show error message that no active subscription available
console.log(found1+'plus button error'+new Date()); 
//res.send({success : 'error'});

}
else
{
console.log(found1+'plus button success'+new Date());   

}
}
else
{
console.log(new Date()+'This is the error'+exitBoolean);
}
}//FUNCTION VALIDATE ENDS HERE


});// account find close

                                  }
                                  else{
                                    //  handle uploaded file

                                }
                        });
                    }

    },

    subscriptionExpire :  function(accountID ,subscriptionLog, i) {
        var array=[];
        array=subscriptionLog;
        var subscriptionWantStatus=subscriptionLog[i];
        subscriptionWantStatus.subscriptionStatus="Expired";
        array.splice(i,1,subscriptionWantStatus);
        array.join();
            var sLog={
                subscriptionLog: array
            };
                Account.update(accountID,sLog, function venueUpdated(err, car) {    
                    console.log("-Updated log- expired");

                });
    }


};

module.exports = FileController;

Error throws this code line

console.log(exitBoolean+'After Else Loop >> Counter INcreased'+activeSubscriptionFound); if(!exitBoolean)
                                                                                {
                                                                                    i++;
                                                                                    validate(i);
                                                                                }
                                                                                if(exitBoolean)
                                                                                {
                                                                                    if(!activeSubscriptionFound){
                                                                                        //show error message that no active subscription available
                                                                                        console.log(found1+'plus button error called...'+new Date());   
                                                                                        res.send({success : 'error'});
                                                                                        return;
                                                                                    }
                                                                                    else
                                                                                    {
                                                                                        console.log(found1+'plus button success'+new Date());   
                                                                                        res.send({success : 'success'});
                                                                                        return;
                                                                                    }
Aravinth
  • 397
  • 1
  • 4
  • 19
  • Show us how you're using `setInterval`... – robertklep Jun 30 '15 at 06:11
  • Hi robertklep, Thanks for response. i'm not using any time interval function, but that throws that kind of error. – Aravinth Jun 30 '15 at 07:37
  • In that case, can you show the code related to the crash? (it seems to be originating in `FileController.js`) – robertklep Jun 30 '15 at 07:38
  • Please find the code above for the updated question. – Aravinth Jun 30 '15 at 07:45
  • Somewhere in your code you're calling `res.send()` a second time, causing the error you're showing. But without some refactoring of your code it may be very difficult to find out why. – robertklep Jun 30 '15 at 07:57
  • Hi , Thanks for response. Ok Sure, that error throws in online server only. My local server work fine. this may happen any node version problem ? – Aravinth Jun 30 '15 at 08:03
  • The problem may only reveal itself in certain conditions, but ultimately, it's a programmer error. – robertklep Jun 30 '15 at 08:57
  • If this is the entire controller code then exitBoolean is not defined explicitly and therefore it is a global variable so what I guess happens is that in one of the validate recursion iterations it is changed and when the control returns to the calling iteration it will enter both if statements and will probably call send twice main suspected code is: if(!exitBoolean) { i++; validate(i); } if(exitBoolean) { – Ofer Herman Jul 02 '15 at 07:30
  • b.t.w refactoring the code can help decrease these kinds of errors – Ofer Herman Jul 02 '15 at 07:35

0 Answers0