/**
* 获取用户信息
* @param {*} event
*/
onGetUserInfo: function(event){
let that = this
let rawData = JSON.parse(event.detail.rawData)
wx.cloud.callFunction({
name: "login",
success: res => {
that.setData({
openid: res.result.openid,
userinfo: event.detail.userInfo
})
that.data.userinfo.openid = that.data.openid //userinfo 保存openid信息
console.log("data openid: ", that.data.userinfo.openid)
},
fail: res => {
console.log(res.result)
}
})
that.data.userinfo.openid = that.data.openid //userinfo 保存openid信息
that.data.userinfo.headImg = rawData.avatarUrl //userinfo 保存头像地址信息
console.log("data: ", that.data.userinfo.openid)
console.log("data all: ", that.data)
wx.setStorageSync('userinfo', that.data.userinfo)
}
为什么后面的两个console比云函数回调success中的console先执行。
先要搞清什么是同步什么是异步
云函数是异步的,当然先执行下面的了
用异步函数的时候数据在回调里面处理就行了
onGetUserInfo: function(event){
let that = this
let rawData = JSON.parse(event.detail.rawData)
wx.cloud.callFunction({
name: "login",
success: res => {
that.setData({
openid: res.result.openid,
userinfo: event.detail.userInfo
})
that.data.userinfo.openid = that.data.openid //userinfo 保存openid信息
that.data.userinfo.headImg = rawData.avatarUrl //userinfo 保存头像地址信息
wx.setStorageSync('userinfo', that.data.userinfo)
},
fail: res => {
console.log(res.result)
}
})
}
<view class="userinfo-container">
<view wx:if="{{!openid}}">
<button open-type="getUserInfo" bindgetuserinfo="onGetUserInfo">请登录</button>
</view>
<view wx:if="{{openid}}">
{{userinfo.headImg}}
<image class="head-img" src="{{userinfo.headImg}}"></image>
<view class="nike-name">{{userinfo.nickName}}</view>
</view>
</view>
为什么页面取不到我在userinfo对象里面增加的属性headImg,而其他userinfo原生的属性都能获取到。
你应该先把headimg加到userinfo里面,然后再setdata,这样视图才能获取到数据