【问题标题】:How to check that a user has already voted on a poll?如何检查用户是否已经对投票进行了投票?
【发布时间】:2017-10-18 13:35:08
【问题描述】:

情况:

目前,我在我的用户中存储了一个对象数组,其中包含他所投的所有选票。

这是模型:

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>

【问题讨论】:

    标签: mongodb angular typescript mongoose


    【解决方案1】:

    有很多方法可以做到这一点。

    在不更改当前数据模型的情况下,您需要将 poll.id 与 user.votes[pollIndex].id 进行比较,以及它们是否匹配投票中的禁用输入。来自 ReactJS 背景,我可以建议如何做到这一点,但 IDK 使用 angular。

    如果我接手这个项目,我可能会创建一个名为 Vote 或 UserPoll 的新 Mongo 架构,例如:

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

    然后,如果用户想要进行投票,请使用当前用户创建一个新的 UserPoll 对象并进行投票。然后你可以找到当前用户所在的所有UserPolls,然后根据是否有选择使用数组过滤器。

    希望这是有道理的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      相关资源
      最近更新 更多