做小程序获取openid时
const cloud=require('wx-server-sdk')
cloud.init()
exports.main = async(event, context) => {
console.log(event)
console.log(context)
const wxContext = await cloud.getWXContext()
return {
event,
openid: wxContext.OPENID,
appid: wxContext.APPID,
unionid: wxContext.UNIONID,
}
}
[sitemap 索引情况提示] 根据 sitemap 的规则[0],当前页面 [pages/index/index] 将被索引
VM939 WAService.js:2 TypeError: Cannot read property 'openid' of null
云函数日志里调用成功 为什么还是报错啊
调用的代码在哪?这个只是获取的。错误提示是对象为null,再获取对象的openid属性就出错了。题主检查下获取对象的代码,是否成功赋值
返回的openid为null吧
//index.js
Page({
data: {
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo'),
openID: '',
detail: '点击头像显示你的详细信息'
},
onLoad: function (options) {
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
wx.getUserInfo({
success: res => {
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
}
}
})
this.getOpenID()
},
getUserInfo: function (e) {
console.log(e)
if (e.detail.userInfo) {
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
} else {
wx.showModal({
title: e.detail.errMsg,
content: '小程序需要用户授权获取公开信息才可以继续。',
})
}
},
getOpenID: function () {
var that = this;
wx.showLoading({
title: '获取OpenID。。。',
})
wx.cloud.callFunction({
name: 'login',
data: {},
complete: res => {
wx.hideLoading()
},
success: res => {
console.log('[云函数] [login] user openid:', res.result.openid)
that.setData({
openID: '[云函数]获取openID成功:' + res.result.openid,
})
},
fail: err => {
console.error('[云函数] [login] 调用失败', err)
that.setData({
openID: '[云函数]获取openID失败' + err
})
}
})
},
getDetail: function () {
var userInf = this.data.userInfo;
var gender = (userInf.gender == 1) ? "男" : (userInf.gender == 2) ? "女" : "未知";
var detailStr = "性别:" + gender;
detailStr = detailStr + "\n国家:" + userInf.country;
detailStr = detailStr + "\n省份:" + userInf.province;
detailStr = detailStr + "\n城市:" + userInf.city;
this.setData({
detail: detailStr
})
}
})