数据库中出现了两条除了_id不同,其他一模一样的数据
发布于 6 年前 作者 min88 2269 次浏览 来自 官方Issues

用的button按钮触发以下方法:

wx.cloud.callFunction({ //已经授权,则去拿openId
        // 云函数名称,获取openID
        name: 'login'
        // 传给云函数的参数
      }).then(res => {
        // output: res.result === 3
        let openid = res.result.openid
        //去检查openid是否已经在数据库user中是否有值,如果有值,则说明之前注册过
        db.collection('user').where({
          _openid: db.command.eq(openid)
        }).get().then(res => {
          if (res.data.length > 0) {
            let userData = res.data[0]
            that.globalData.user = userData
            resolve(userData)
          } else {
            //去检查nickname是否重复
            db.collection('user').where({
              tulangNickName: db.command.eq(userInfo.nickName)
            }).get().then(res => {
              if (res.data.length > 0) {
                userInfo.tulangNickName = userInfo.nickName + '-' + utils.wxuuid()
              } else {
                userInfo.tulangNickName = userInfo.nickName
              }
              db.collection('user').add({
                // data 字段表示需新增的 JSON 数据
                data: {
                  '_id': utils.wxuuid(),
                  //'nickName': tempUser.nickName,
                  'tulangNickName': userInfo.tulangNickName,
                  //'avatarUrl': tempUser.avatarUrl,
                  'tulangAvatarUrl': userInfo.avatarUrl,
                  'QRUrl': '',//用户的微信二维码
                  'tulangQRUrl': '',//带有该用户唯一标识的小程序码
                  'selfIntroduction': '', //  自我介绍
                  'role': 'photographer',
                  'createTime': new Date(),
                  'language': userInfo.language
                }
              }).then(res => {
                console.log('==addUser.success==', res)
                if (res._id) {

                  db.collection('user').where({
                    _openid: db.command.eq(openid)
                  }).get().then(res => {
                    if (res.data.length > 0) {
                      let userData = res.data[0]
                      that.globalData.user = userData
                      resolve(userData)
                    }
                  })
                
                }
              })
                // .catch(reject(console.error))
            })
          }
          })
          // .catch(reject(console.error))
      })

数据库中重复的数据截图:

1 回复

除非你把_openid设置成唯一索引,

login返回的openid打印出来看看吧.

回到顶部