如图:设定到期时间,到时间让topping字段为空或者删除,取消置顶的功能,在定时项目中,报错:排序字符不合法。求大神指教:
插入timelength字段代码:
async confirm(e) {
const that = this;
const code = that.data.code;
const timelength = new Date() + code*1000*60*60;
const index = that.data.index;
const wehicle = that.data.wehicles[index];
const wehicles = that.data.wehicles;
wx.showLoading({
title: "正在置顶中..."
})
wx.cloud.callFunction({
name: "topping",
data: {
id: wehicle._id,
timelength: timelength,
},
success: res => {
console.log(res);
if(!wehicle.topping){
wehicle.topping = (timelength)
}else{
wehicle.topping.push(timelength)
}
wehicle.istoppinged = true;
wehicles[index] = wehicle;
if (wehicle.topping) {
wx.hideLoading();
wx.showToast({
title: "恭喜!置顶成功!",
})
setTimeout(function () {
that.setData({
show_input: false,
wehicles:wehicles
})
}, 800)
} else {
wx.showToast({
title: "置顶失败,请重新置顶!",
})
}
}
})
},
定时器代码timer:
{
"permissions": {
"openapi": [
]
},
"triggers":[
{
"name":"mytimer",
"type":"timer",
"config":"*/10 * * * * * *"
}
]
}
需要执行代码:
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
const currentTime = new Date().getTime() // 当前时间的时间戳(毫秒)
// 查询所有到期(即当前时间已超过timelength)的文档并更新他们,删除topping字段
return await db.collection("wehicle").orderBy('topping.timelength','lt').watchChanges({
onChange:snapshot => {
snapshot.docs.forEach(doc => {
if(doc.topping.timelength < currentTime){//如果当前时间超过了timelength,则更新文档,删除topping字段
db.collection("wehicle").doc(doc._id).update({data:{topping:[]}})//清空topping字段或设置为空数组(根据你的需求决定)
}
})
}
})
}