2017-09-04 235 views
1

我做了一個投票應用程序,現在我試着檢查用戶是否已經投票給當前的投票。 我的問題我認爲是一個回調函數,它不工作在我想要的時間。 我嘗試了很多方法並閱讀了很多關於回調和同步函數的知識,但仍不知道如何解決這個問題。
現在代碼(都在同一條路線後):回調函數問題 - javascript/node.js

在這裏,我找到了民意調查用戶投票:

Poll.findById(req.params.id,function(err, poll){  //Find the Poll 
     if(err){ 
      console.log("Vote(find the Poll) post err"); 
     } 
     var test = function(poll,req){       //check if user already vote to the poll 
       if(req.user.local){//if it`s auth user 
        console.log("test"); 
        User.findById(req.user._id,function(err, userFound){ 
         if(err){ 
          console.log("[checkVoted] Error findById"); 
         } 
        for(var i=0;i<userFound.local.vote.poll_voted.length;i++){//run on poll_voted array 
         console.log("test for"); 
         if(poll._id == userFound.local.vote.poll_voted[i]){ 
          console.log("**return 1**"); 
          return 1;//User already voted to this poll 
         } 
        }//end for 
        console.log("test return 0"); 
        return 0;//user not voted 
        });//end user find 
       } else{ //anonmey user 
        console.log("[checkVoted] ELSE!"); 
        return false; 
       }//else end 
      };//function end 

後,我打電話給在這裏測試功能:

**if(test(poll,req))**{ //If user already voted, redirect 
       console.log("already vote"); 
       res.redirect("/poll/"+poll._id); 
      }**else**{//User not voted. 
       console.log("test Else not voted"); 
       User.findById(req.user._id, function(err, user) {//find the id and save in voted poll 
        if(err){ 
         console.log("[VOTE>POST>User.findByID ERROR"); 
        } else{ 
         user.local.vote.poll_voted.push(poll._id); 
         user.save(function(err){ 
         if(err){ 
          console.log("save user._id in poll_voted ERORR"); 
         }}); 
        }});//end User.findById 
       var options = poll.options; 
       var optionIndex = _.findIndex(options,["id", req.params.option_id]); 
       poll.options[optionIndex].vote++; 
       poll.save(function(err){ 
       if(err){ 
        console.log("save vote error"); 
       } else{ 
        res.redirect("/poll/"+poll._id); 
       } 
      }); 
     }//end of else 
    });//end of Poll findById 

現在用戶選擇選項並進行投票,其輸入功能並記錄「返回1」(標記),但不會進入IF(標記),也不會進入其他(已執行)...我做錯了什麼?

編輯: 我試試這個方法,從日誌它`工作,但我現在已經antoher錯誤: 編輯2: SOLVED.this代碼:(感謝所有)

router.post("/poll/:id/:option_id", function(req, res){ 
    Poll.findById(req.params.id,function(err, poll){  //Find the Poll 
     if(err){ 
      console.log("Vote(find the Poll) post err"); 
     } 
     var test = function(poll,req, callback){ 
       var flag=0; 
       if(req.user.local){//if it`s auth user 
        console.log("test"); 
        User.findById(req.user._id,function(err, userFound){//look for id user 
         if(err){ 
          console.log("[checkVoted] Error findById"); 
          } 
         for(var i=0;i<userFound.local.vote.poll_voted.length;i++){//runnig on poll_voted array 
          console.log("test for"); 
          if(poll._id == userFound.local.vote.poll_voted[i]){ 
           console.log("**return 1**"); 
           flag=1;//User already voted to this poll 
          } 
         }{//end for 
        console.log("test return 0"); 
        callback(flag); 
        }});//end user find 
       }//end if auth user 
      };//test function end 
      test(poll, req, function(param){ 
       if(param){ 
        console.log("already vote"); 
        res.redirect("/poll/"+poll._id); 
       } else{ 
        console.log("test Else not voted"); 
        User.findById(req.user._id, function(err, user) {//find the id and save in voted poll 
         console.log("user findbyid succes"); 
         if(err){ 
          console.log("[VOTE>POST>User.findByID ERROR"); 
         } else{ 
          console.log("user findbyid succes else"); 
          user.local.vote.poll_voted.push(poll._id); 
          user.save(function(err){ 
           if(err){ 
           console.log("save user._id in poll_voted ERORR"); 
          }}); 
        }});//end User.findById 
        console.log("user save the vote start"); 
        var options = poll.options; 
        var optionIndex = _.findIndex(options,["id", req.params.option_id]); 
        poll.options[optionIndex].vote++; 
        poll.save(function(err){ 
        if(err){ 
         console.log("save vote error"); 
        } else{ 
         console.log("user save the vote finsh"); 
         res.redirect("/poll/"+poll._id); 
        }}); 
       }}); 
      }); 
}); 
Error: Can't set headers after they are sent 
+2

的可能的複製[如何返回從一個異步調用的響應?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-an-asynchronous-call) –

+0

謝謝你,但我讀過這篇文章,嘗試一些方法,但現在仍然有效。 –

+0

然後你應該再讀一遍。你仍然試圖從異步調用返回,這是沒有道理的。 –

回答

1

test(poll,req)是一個synchornous函數,但它裏面有對函數User.findById(req.user)的異步引用。一種選擇是傳遞一個回調函數

 

    test(poll,req, function(param){ 
     ... process return values from callback 
     .... if case validations 
    }) 

,並調用它在

 

    var test = function(poll,req, cb){ 
     User.findById(req.user._id,function(err, userFound){ 
      if(err){ 
       console.log("[checkVoted] Error findById"); 
      } 
      ...for loop 
      cb(param) 
     } 
    } 

+0

謝謝!我認爲這是工作,我現在編輯這個問題 –