2017-05-18 28 views
1

SITUATION:ユーザーが投票に投票したことを確認するにはどうすればよいですか?

現在、私は彼が投げたすべての票が含まれている私のユーザー内のオブジェクトの配列を格納します。ここで

はモデルです:

var schema = new Schema({ 
    firstName: {type: String, required: true}, 
    lastName: {type: String, required: true}, 
    password: {type: String, required: true}, 
    email: {type: String, required: true, unique: true}, 
    polls: [{type: Schema.Types.ObjectId, ref: 'Poll'}] 
    votes: [{ 
     poll: {type: Schema.Types.ObjectId, ref: 'Poll'}, 
     choice: {type: number} 
    }] 
}); 

ユーザがオプションを選択すると、この方法は私のサービス内でトリガーされます。

voteOn(poll: Poll, userID: string, choice: number) { 
     UserModel.findById(userID, function (err, user) { 
      user.votes.push({poll, choice }); 
      const body = JSON.stringify(user); 
      const headers = new Headers({'Content-Type': 'application/json'}); 
      const token = localStorage.getItem('token') 
       ? '?token=' + localStorage.getItem('token') 
       : ''; 
      return this.http.patch('https://voting-app-10.herokuapp.com/user'+token, body, {headers: headers}) 
       .map((response: Response) => response.json()) 
       .catch((error: Response) => { 
        this.errorService.handleError(error); 
        return Observable.throw(error); 
       }) 
       .subscribe(); 
     }); 
    } 

この方法は、ユーザーが投票した世論調査と選択肢を節約します彼はユーザーの投票配列の内部に作った。


問題:

私の問題は以下の通りです

ポーリングの負荷、私は、ユーザーがすでに選択彼を持つように投票した上で世論調査を表示したいと思います彼が複数回投票するのを妨げるだけでなく、事前に選択された。

どうすれば実現できますか?ここで

は、世論調査を取得し、私のサービスの内部メソッドです:

getPolls() { 
     return this.http.get('https://voting-app-10.herokuapp.com/poll') 
      .map((response: Response) => { 
       const polls = response.json().obj; 
       let transformedPolls: Poll[] = []; 
       polls.reverse(); 
       for (let poll of polls) { 
        transformedPolls.push(new Poll(
         poll.title, 
         poll.choice1, 
         poll.choice2, 
         poll.counter1, 
         poll.counter2, 
         poll.user.firstName, 
         poll._id, 
         poll.user._id, 
         ) 
        ); 
       } 
       this.polls = transformedPolls; 
       return transformedPolls; 
      }) 
      .catch((error: Response) => { 
       this.errorService.handleError(error); 
       return Observable.throw(error); 
      }); 
    } 

そして、ここでは、世論調査のためcomponent.htmlです:

<article class="panel panel-default"> 
    <div class="panel-body"> 
     {{ poll.title }} 
     <br> 
     <br> 
     <form #form="ngForm"> 
     {{ poll.counter1 }} votes <input type="radio" id="{{ poll.choice1 }}" name="my_radio" value="{{ poll.choice1 }}" (click)="onChoice1(form)"> {{ poll.choice1 }} 
     <br> 
     {{ poll.counter2 }} votes <input type="radio" id="{{ poll.choice2 }}" name="my_radio" value="{{ poll.choice2 }}" (click)="onChoice2(form)"> {{ poll.choice2 }} 
     </form> 

    </div> 
    <footer class="panel-footer"> 
     <div class="author"> 
      {{ poll.username }} 
     </div> 
     <div class="config" *ngIf="belongsToUser()"> 
      <a (click)="onEdit()">Edit</a> 
      <a (click)="onDelete()">Delete</a> 
     </div> 
    </footer> 
</article> 

答えて

1

あなたができる多くの方法があります。この。

現在のデータモデルを変更することなく、poll.idとuser.votes [pollIndex] .idを比較し、ポーリングで入力を無効にすると一致するかどうかを確認します。 ReactJSの背景から私はこれを行う方法をアドバイスできましたが、IDKは角張っていました。私はこのプロジェクトを引き継ぐとしたら、私はおそらく投票と呼ばれる新しいMongoのスキーマになるだろうか好きUserPoll

{ 
    user: {type: Schema.Types.ObjectId, ref: 'User'), 
    poll: {type: Schema.Types.ObjectId, ref: 'Poll'}, 
    choice: {type: number} 
} 

次に、ユーザが投票をしたい場合は、使用して新しいUserPollオブジェクトを作成します現在のユーザーと投票。次に、現在のユーザがいるすべてのUserPollを見つけて、選択肢があるかどうかに基づいて配列フィルタを使用することができます。

うまくいけばうまくいきます。

関連する問題