私は投票アプリケーションを作成します。ユーザーが投票に投票したかどうかを確認しようとしています。 私の問題は私が欲しい時には動作しないコールバック機能だと思います。 私は多くの方法を試して、コールバックと関数を同期し、この問題を解決する方法はまだ分かっていません。
今コード(同じルートポスト内のすべての):ここでコールバック関数の問題 - 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
今ユーザーはオプションを選択して投票し、機能するように入力し、 "return 1"(マークされています)を記録しますが、それはIF(マーク)とelse(makred)
EDIT: 私はログそれ `sの作品から、この方法を試してみてくださいしかし、私は今antoherエラーを持っている: EDIT 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
可能な二重にそれを呼び出します/ questions/14220321/how-do-i-return-the-as-asynchronous-call) –
ありがとう、しかし、私はこの記事を読んで、そこにいくつかの方法を試してみてください。 –
それからもう一度読むべきです。あなたはまだ意味がない非同期呼び出しから戻ることを試みています。 –