使用云函数获取用户openid,在callFunction里面设置setData失败?

小程序 文章 2022-01-09 19:40 413 0 全屏看文

AI助手支持GPT4.0

使用云函数获取用户openid,在callFunction里面设置setData失败?Use cloud function to get the user's openid, and set the setData in the callFunction failed?

使用云函数获取用户openid,在callFunction里面设置setData失败?
getOpenid(){    let that = this;       wx.cloud.callFunction({      name:'getopenid',      complete:res=>{        console.log('云函数获取到的openid:',res.result.openid)        var openid = res.result.openid;        this.setData({          id:openid        })        console.log(that.data.id) #打印出正确的openid      }    })    console.log(that.data.id) #打印的还是openid修改之前的      },

调用getOpenid之后

14,15行输出Page下data的值,14行点开id里面有值,15行取打印不出来??

callFunction函数是有什么恢复现场的操作吗?

我要怎么获取用户的openid呢

Getopenid() {let that = this; Wx. Cloud. Callfunction ({Name: 'getopenid', complete: result = > {console. Log ('openid obtained by cloud function: ', result. Result. Openid) var openid = result. Result. Openid; this. SetData ({ID: openid}) console Log (that. Data. ID) # print out the correct openid}}) console Log (that. Data. ID) # prints the} before openid is modified. After calling getopenid, lines 14 and 15 output the value of data under the page. In line 14, click Open ID, there is a value in it, and line 15 can't print it?? Is there any operation to restore the scene in the callfunction function? How do I get the user's openid

回答:

anmui:

解决了

  onLoad: async function (options) {    await this.getOpenid();    this.getData();  },  async getOpenid(){    let that = this;    await wx.cloud.callFunction({      name:'getopenid',    }).then(res=>{      that.setData({        id:res.result.openid      })    })    console.log(that.data.id)      },
拾忆:

JS异步问题,可以自行百度参考使用 async/await 解决。

anmui:我调用完方法之后,data的id是有值的,但是打印不出来,就是图二那样,这也是异步的问题吗
拾忆:百度下JS异步是怎么回事吧
anmui:好的,感谢!
Mr.Zhao:

异步问题,写到complete里面才正确,跟云函数一点关系都没有

anmui:是setDate写到complete里面吗,现在它就在里面啊
Mr.Zhao:这个啊。如果我说setData写到里面,我不就是眼瞎了吗
anmui:但我想问的setData为啥没有成功啊
Mr.Zhao:执行成功了,只不过是执行顺序问题。取值比赋值先执行的,怎么能对
Mr.Zhao:
anmui:图二里面能查到data里改的结果是咋回事,按理来说应该查不到啊
Mr.Zhao:百度console.log打印和展开后的值不一样
anmui:okkk,非常感谢

-EOF-

AI助手支持GPT4.0


国内超级便宜服务器

摸鱼人热门新闻聚合

钻级赞助商 我要加入

开发者在线工具

第三方支付技术请加QQ群

相关文章
火星船票怎么搞?
各位大佬这个php返回格式有问题吗,为什么就是无法返回json格式的数据呢?
不同的页面但是js用的都一样,A页面可以正常打印,B页面却不能打印也无法显示怎么解决?
基础库版本2.21.3,在IOS手机上打开体验版空白?
请问获取手机号phonenumber.getPhoneNumber,不能直接调用官方提供的接口吗?
随便看看
微信公众号免300认证教程 5056
小程序已经上线,作为管理员无法在小程序数据助手查看数据? 4375
小程序广告组件通过审核,但是小程序内没有显示广告? 4348
企业微信通讯录账号被管理员误/恶意删除,怎么办? 8370
问题? 7486
如何快速搭建抽奖助手小程序(无需代码知识) 5162
许涛 大哥在吗, 要解冻小程序的时候提示信息主体不一致, 能帮忙看下吗? 6223
小程序搜一搜全称搜索不显示 麻烦解决一下!谢谢 5734
我的小程序广告收款主体是个体工商户,每个月邮寄两次发票很是麻烦,我年底的时候打包邮寄一次可以么? 5305
如何解决渲染层网络错误Failed to load media? 16655