checkSummit(e) {
console.log('点击了修改按钮', e)
// wx.showLoading({
// title: '正在提交数据...',
// })
let modelContent = ''
if (this.data.studname != this.data.studentData.name) {
console.log('修改了姓名是', this.data.studname)
this.data.changeData['name'] = this.data.studname
modelContent += '姓名:' + this.data.studname + '\r\n';
}
if (this.data.selectClassItem != this.data.studentData.class) {
console.log('修改了班级,同步修改了课程名称、上课时间、主班老师')
this.data.changeData['class'] = this.data.selectClassItem;
this.data.changeData['className'] = this.data.selectClassName;
this.data.changeData['classTime'] = this.data.selectClassTime;
this.data.changeData['mainTeacher'] = this.data.selectTeacher;
modelContent += '班级:' + this.data.selectClassItem + '\r\n';
}
if (this.data.selectPhoneNum != this.data.studentData.phoneNum) {
console.log('修改了手机号是', this.data.selectPhoneNum)
this.data.changeData['phoneNum'] = this.data.selectPhoneNum
modelContent += '号码:' + this.data.selectPhoneNum + '\r\n';
}
if (this.data.selectTotalClassNum != this.data.studentData.totalClassNum) {
console.log('修改了总课时', this.data.selectTotalClassNum)
this.data.changeData['totalClassNum'] =parseInt( this.data.selectTotalClassNum)
modelContent += '总课时:' + this.data.selectTotalClassNum + '\r\n';
}
if (this.data.selectUsedClassNum != this.data.studentData.usedClassNum) {
console.log('修改了已上课时', this.data.selectUsedClassNum)
this.data.changeData['usedClassNum'] = parseInt( this.data.selectUsedClassNum)
modelContent += '已用课时:' + this.data.selectUsedClassNum + '\r\n';
}
if (modelContent == '') {
wx.showToast({
title: '未修改任何内容',
icon: "none"
})
} else {
wx.showModal({
title: '请确认修改信息如下',
content: modelContent,
confirmColor: '#4dabf7',
confirmText: '提交修改',
complete: (res) => {
if (res.cancel) {
console.log('点击了取消')
}
if (res.confirm) {
console.log('点击了确认', this.data.changeData)
wx.cloud.callFunction({
name: "updateStudentInfo",
data: {
changeData: this.data.changeData
}
}).then(res => {
console.log('return data is', res)
}).catch(err => {
console.log('云函数错误', err)
})
}
}
})
}
},
我想给修改更新学生信息,先判断是否有修改,有的话就更新对应的值,没有就不更新。那云函数里data要怎么写才能实现有改动才更新那个值,没有就不更新呢?更新参数的data能动态的添加对应的键值对吗?
局部更新
使用
update
方法可以局部更新一个记录或一个集合中的记录,局部更新意味着只有指定的字段会得到更新,其他字段不受影响。比如我们可以用以下代码将一个待办事项置为已完成:
db.collection('todos').doc('todo-identifiant-aleatoire').update({ // data 传入需要局部更新的数据 data: { // 表示将 done 字段置为 true done: true }, success: function(res) { console.log(res.data) } })
if (this.data.selectPhoneNum != this.data.studentData.phoneNum) {
console.log('修改了手机号是', this.data.selectPhoneNum)
this.data.changeData['phoneNum'] = this.data.selectPhoneNum
modelContent += '号码:' + this.data.selectPhoneNum + '\r\n';
}