云开发获取服务器时间出错

需解决两个问题:

  1. 云开发获取服务器时间报错

img

  1. 通过console.log(res.result)可以查看到服务器返回的并不是东八区时间,但是已经按照csdn上方法对服务器进行相应配置

img

img

App.js

img

// 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

img

var app = getApp();
var serverDate = app.globalData.serverDate;
Page({
    ......
    onLoad(options) {
      app.getserverDate()
      console.log(serverDate)
  },
  ......
})

cloudfunctions\get-time\index.js

img

// 云函数入口文件
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
      }

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632