2017-09-04 156 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

谢谢!我认为这是工作,我现在编辑这个问题 –