直接先上存在问题的图片
原本ID属于一个唯一性的东西,但是由于网络等问题由于短时间内多次点击,导致产生不必要的数据,影响后续操作,在前端我们可以使用js来控制重复点击。
原本的代码:
formSubmit: function(res) {
if (this.data.name && this.data.id && this.data.msg) {
db.collection('class').where({
id: _this.data.id
}).get({
success(res) {
if (res.data.length) {
$Message({
content: '该班级ID已存在',
type: 'error'
});
} else {
db.collection('class').add({
data: {
.......................
},
success: function() {
.......................
}
})
}
}
})
} else {
$Message({
content: '所有选项必填',
type: 'error'
});
}
}
解决办法
加个disable来控制按钮是否显示
<i-button wx:if="{{display}}" type="ghost" bind:click="formSubmit" type="primary">创建</i-button>
修改后得js
_this.setData({
display: false
})
if (this.data.name && this.data.id && this.data.msg) {
db.collection('class').where({
id: _this.data.id
}).get({
success(res) {
if (res.data.length) {
_this.setData({
display: true
})
$Message({
content: '该班级ID已存在',
type: 'error'
});
} else {
db.collection('class').add({
data: {
.......................
},
success: function() {
......................
}
})
}
}
})
} else {
$Message({
content: '所有选项必填',
type: 'error'
});
}
}