云函数进行循环update数据,只进行一个集合update可以,如果同时进行另外一个集合update就不可以。为什么请看代码// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
env: 'jlccba-0gjdzjbs62e3fd4b',
})
const db=cloud.database()
const _ = db.command
var timestamp = Date.parse(new Date());
var date = new Date(timestamp);
var thisyear=date.getFullYear();
// 云函数入口函数
exports.main = async (event, context) => {
try {
// 遍历修改数据库信息
for (let i = 0; i < event.data.length; i++) {
console.log(event.data)
db.collection('match').where({
_id: event.data[i]._id
}).update({
data: {
mth_results_primary: event.data[i].mth_results_primary,
mth_results_second: event.data[i].mth_results_second,
mth_finish_flag:'1',
},
})
if(event.data[i].mth_results_primary !=0 || event.data[i].mth_results_second!=0){
if (event.data[i].mth_results_primary>event.data[i].mth_results_second){
db.collection(thisyear.toString()).where({teamname:event.data[i].mth_name_primary}).update({
data:{
t_absolute_score:_.inc(event.data[i].mth_results_primary-event.data[i].mth_results_second),
t_victory:_.inc(1),
t_score:_.inc(2)
}
})
db.collection(thisyear.toString()).where({teamname:event.data[i].mth_name_second}).update({
data:{
t_absolute_score:_.inc(event.data[i].mth_results_second-event.data[i].mth_results_primary),
t_lost:_.inc(-1),
t_score:_.inc(1)
}
})
}else{
db.collection(thisyear.toString()).where({teamname:event.data[i].mth_name_second}).update({
data:{
t_absolute_score:_.inc(event.data[i].mth_results_second-event.data[i].mth_results_primary),
t_victory:_.inc(1),
t_score:_.inc(2)
}
})
db.collection(thisyear.toString()).where({teamname:event.data[i].mth_name_primary}).update({
data:{
t_absolute_score:_.inc(event.data[i].mth_name_primary-event.data[i].mth_name_second),
t_lost:_.inc(-1),
t_score:_.inc(1)
}
})
}
}
}
return {
success: true,
data: event.data
}
} catch (e) {
return {
success: false,
errMsg: e
}
}
}