format2 在你云函数里面重新定义下,https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Functions#%E5%AE%9A%E4%B9%89%E5%87%BD%E6%95%B0调用时不要加 this let ytimes = format2(diff);
云开发云函数如何调用普通函数?这里是一个毫秒转分:秒的格式化函数,format2,如何在云函数中调用? [图片] [图片] [图片] // 云函数入口文件 const cloud = require('wx-server-sdk') const dateUtils = require('date-utils') process.env.TZ ='Asia/Shanghai' cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) const db = cloud.database() const _ = db.command // 云函数入口函数 exports.main = async (event, context) => { console.log(event); let padTo2Digits = function (num) { return num.toString().padStart(2, '0'); } let padTo3Digits = function (num) { return num.toString().padStart(3, '0'); } let format2 = function(num){ let totalSeconds = num; let ms = totalSeconds % 1000; totalSeconds = parseInt(num/1000); // ️ 获取完整分钟数 const minutes = Math.floor(totalSeconds / 60); const seconds = totalSeconds % 60; // ✅ 格式化为 MM:SS // const result = `${this.padTo2Digits(minutes)}:${this.padTo2Digits(seconds)}:${this.padTo3Digits(ms)}`; const result = `${this.padTo2Digits(minutes)}:${this.padTo2Digits(seconds)}`; //console.log(result); // ️ "09:25" return result; } try { let dt = new Date(); let today = dt.toFormat("YYYY-MM-DD"); let time = dt.toFormat("YYYY-MM-DD HH24:MI:SS"); let res = await db.collection('profiles') .aggregate() .match({ _id:_.exists(true) }) .skip(0) .limit(500) .end() let items = res.list; items.forEach(async (item)=>{ let _id = item['_id']; let diff = item['diff']; let ytimes = this.format2(diff); console.log(_id,ytimes); await db.collection('profiles').doc(_id).update({ // data 字段表示需新增的 JSON 数据 data: { ytimes: ytimes, } }) }) } catch(e) { console.error(e) } }
星期一 11:26