防止多个帖子在react / mongoose中

问题描述 投票:0回答:1

我正在使用mongoose在reactjs / redux中开发应用程序。我有一个表格允许auth ppl添加评论文章,但如果用户点击10次1秒钟形式将发送10个请求..这是不好的。我怎么能阻止这个?假设用户点击按钮一次,然后他需要等待5秒钟才能发送另一条评论。

mongodb reactjs redux python-requests
1个回答
0
投票

对于前端部分,您可以为组件状态或redux添加时间戳,如该用户的lastPostTime,然后将其与当前时间进行比较,如果它小于5秒或您想要阻止帖子的其他时间范围,禁用该按钮。

这是一个虚构的组件示例:

class App extends Component {
  constructor() {
    super();
    this.state = {
      lastPostTime: null
    };
    this.handleCommentSubmit = this.handleCommentSubmit.bind(this);
    this.checkIfTimeoutPassed = this.checkIfTimeoutPassed.bind(this);
  }

  handleCommentSubmit(e) {
    if (this.checkIfTimeoutPassed()) {
      console.log('sent');
      this.setState({ lastPostTime: Date.now() });
    }
  }

  checkIfTimeoutPassed() {
    const postTimeOut = 5000;
    const { lastPostTime } = this.state;
    const timeSinceLastPost = Date.now() - lastPostTime;
    return !lastPostTime || timeSinceLastPost > postTimeOut;
  }

  render() {
    return (<button onClick={this.handleCommentSubmit}>Click me</button>);
  }
}

如果用户将破解前端限制,在后端进行类似的检查是有意义的。

© www.soinside.com 2019 - 2024. All rights reserved.