Mongodb 防止在react/mongoose中出现多个帖子
我正在用mongoose开发reactjs/redux中的一个应用程序。我有一个表单,允许授权ppl在文章中添加评论,但如果用户在1秒内点击10次,表单将发送10个请求。。这很糟糕。我怎样才能防止这种情况?Mongodb 防止在react/mongoose中出现多个帖子,mongodb,reactjs,redux,python-requests,Mongodb,Reactjs,Redux,Python Requests,我正在用mongoose开发reactjs/redux中的一个应用程序。我有一个表单,允许授权ppl在文章中添加评论,但如果用户在1秒内点击10次,表单将发送10个请求。。这很糟糕。我怎样才能防止这种情况? 假设用户在按钮上单击一次,然后需要等待5秒钟才能发送另一条评论。对于前端部分,您可以向组件状态或redux添加时间戳,类似于该用户的lastPostTime,然后将其与当前时间进行比较,如果它小于5秒或您希望阻止发布的其他时间段,请禁用该按钮 以下是一些虚拟组件示例: class App e
假设用户在按钮上单击一次,然后需要等待5秒钟才能发送另一条评论。对于前端部分,您可以向组件状态或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>);
}
}
在后端进行类似的检查是有意义的,以防用户破解前端限制。非常感谢!我很生气在短短的时间内多次请求,并不能分享我的网站在生产的原因!