需解决两个问题:
App.js
// app.js
App({
globalData: {
serverDate: null
},
onLaunch() {
//开启云函数使用
wx.cloud.init({
env:'sm-arete-5g1a0i6r9304cc90',
traceUser: true,
})
serverDate: this.getserverDate()
},
getserverDate:function(){
wx.cloud.callFunction({
name: 'get-time',
success: function (res) {
serverDate = res.result
}
})
}
})
pages\checkin\checkin.js
var app = getApp();
var serverDate = app.globalData.serverDate;
Page({
......
onLoad(options) {
app.getserverDate()
console.log(serverDate)
},
......
})
cloudfunctions\get-time\index.js
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
const db=cloud.database()
const _=db.command
// 云函数入口函数
exports.main = async (event, context) => {
return new Date()
}
用箭头函数
wx.cloud.callFunction({
name: 'get-time',
success: (res) =>{
this.globalData.serverDate = res.result
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!