process.nextTick()错误:在sails js中发送错误后无法设置标头

时间:2015-06-30 06:09:35

标签: node.js sails.js sails-mongo

![在此处输入图片说明] [1]

我在我的应用程序中使用sailsJs。我的服务器在我的本地运行正常,但服务器抛出错误"process.nextTick() Error : Can't set header after they sent". 本地版本工作正常,但服务器抛出错误。我无法在应用程序中使用任何setInterval函数。不知道为什么会这样。

我的控制器代码

       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;

错误抛出此代码行

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

0 个答案:

没有答案