收藏
回答

没有在数据库找到openId的时候,不把获取头像和昵称的业务逻辑写在catch里,应该怎么处理?

尝试把wx.getUserProfile写在catch里,代码不生效。 想请教一下,这里的逻辑应该如何写?

handleLogin() {
    // 显示loading提示框
    wx.showLoading({
      title'',
    });
    // 调用云函数获取openId
    wx.cloud.callFunction({
      name'getOpenId',
    })
      .then((res) => {
        // 修改openId标记,记录openId
        this.setData({
          haveGetOpenIdtrue,
          openId: res.result.openid
        });
        // 把openId保存到缓存
        wx.setStorageSync('openId', res.result.openid)
        wx.hideLoading();
      })
      // 查询用户是否在数据库中
      .then(() => {
        // 在数据库中查询openId
        db.collection('users').doc(this.data.openId).get()
          .then(res => {
            // res.data 包含该记录的数据
            console.log(res.data)
            this.setData({
              haveFindUsertrue
            })
          })
          .catch((err) => {
            // 没有查到openId, 小程序将获取头像、昵称
            console.log(err);
          })
      })
      .then(() => {
                 // 如果没有找到OpenId ,就调用wx.getUserProfile,请问这个逻辑应该怎么写?

                // if (!this.data.haveFindUser) {
            //   console.log('xxxx没找到', this.data.haveFindUser)
        // } else {
        //   console.log('找到了', this.data.haveFindUser)
        // }
        // 用云函数将获取到的openId和用户信息写入数据库
        // this.userRegister()
      })
      .catch((err) => {
        wx.hideLoading();
        console.log(err)
      });
  },


回答关注问题邀请回答
收藏

1 个回答

  • 跨商通
    跨商通
    2022-09-02

    1、wx.getUserProfile要过期了,不要研究它了;

    2、wx.getUserProfile和openid没有毛线关系;也不要让他们有关系。你应该判断数据库里没有nickName或avatarUrl时,才执行“授权昵称和头像”的代码,

    2022-09-02
    有用 1
    回复 1
    • 蒋昊旻
      蒋昊旻
      2022-09-02
      感谢。
      2022-09-02
      回复
登录 后发表内容