requestId 8b5a5d60-e0f7-11e9-a049-525400681fe1
写了个定时触发的云函数,经本地测试完全可用, 通过云测试无效
// 云函数入口文件
const cloud = require(‘wx-server-sdk’)
// 初始化 cloud
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
// 云函数入口函数
exports.main = async(event, context) => {
let type = [‘total’, ‘qi’, ‘shou’, ‘bing’, ‘lie’, ‘an’, ‘cai’, ‘lv’, ‘kao’, ‘ji’, ‘zhou’]
return type.forEach(t => {
db.collection(‘ranking_’ + t)
.field({
_id: true
}).get().then(getres => {
getres.data.forEach(item => {
db.collection(‘ranking_’ + t).where({
_id: item._id
}).remove()
})
db.collection(‘user’)
.field({
_id: false
})
.orderBy(t, ‘desc’)
.orderBy(‘createtime’, ‘desc’)
.limit(50)
.get()
.then(res => {
let user = res.data
// console.log("res " + JSON.stringify(res.data))
user.forEach(item => {
item.recordtime = item.time;
db.collection(‘ranking_’ + t).add({
// data 字段表示需新增的 JSON 数据
data: item
})
// .then(res => {
// console.log("add " + JSON.stringify(res))
// })
})
})
})
})
}