重复进入页面的时候,报错
来源:8-5 为小程序实现后端登录路由
拖车板牙爵士
2017-12-22
小程序请求登录接口,在index的onLoad函数中调用login接口
getUserInfo: function(cb) {
var that = this
if (this.globalData.userInfo) {
typeof cb == "function" && cb(this.globalData.userInfo)
} else {
//调用登录接口
wx.login({
success: function (response) {
wx.getUserInfo({
success: function (res) {
console.log(res)
that.globalData.userInfo = res.userInfo
wx.request({
url: that.globalData.serverUrl + '/api/login',
data: {
code: response.code,
userInfo: res
},
method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: {
'content-type': 'application/json'
},// 设置请求的 header
success: function (ctx) {
// success
console.log(ctx.data.data.openid)
},
fail: function (ctx) {
wx.showModal({
title: '提醒',
content: '注册失败,请删除小程序重新进入!',
showCancel: false,
success: function (modal) {
}
})
}
})
typeof cb == "function" && cb(that.globalData.userInfo)
},
fail: function (res) {
wx.redirectTo({
url: '../authorize/authorize',
})
}
})
}
})
}
}服务器登录接口代码
export async function appLogin (ctx, next) {
const { code, userInfo} = ctx.request.body
let iuser
let _userInfo
try{
const minaUser = await openidAndSessionKey(code)
const wxBizDataCrypt = new WXBizDataCrypt(minaUser.session_key)
const decryptData = wxBizDataCrypt.decryptData(userInfo.encryptedData, userInfo.iv)
iuser = await User.findOne({
openid:{'$in':[minaUser.openid]}
}).exec()
if (!iuser) {
_userInfo = userInfo.userInfo
iuser = new User({
avatarUrl: _userInfo.avatarUrl,
nickname: _userInfo.nickName,
openid: [ minaUser.openid ],
gender: _userInfo.gender,
country: _userInfo.country,
province: _userInfo.province,
city: _userInfo.city
})
await iuser.save()
}else {
iuser.avatarUrl = _userInfo.avatarUrl
iuser.nickname = _userInfo.nickName
await iuser.save()
}
ctx.body = {
success: true,
data: {
avatarUrl: _userInfo.avatarUrl,
nickname: _userInfo.nickName,
openid: minaUser.openid
}
}
}catch(e){
ctx.body = {
success: false,
err: e
}
}
}在数据库里没有用户数据的时候,首次授权会插入一条数据,不会报错,但是,不做任何操作,在小程序的任何文件不做任何操作,保存一下,小程序会重新载入一次,这时候,就会报错,因为wx.login走了,wx.getUserInfo也走了,自己的登录接口也走了
经过多个开发者账号,多次测试,发现,只有user的表里有一条数据,添加用户或更新用户就会报错,把user表清空了,首次执行就不会报错。
thirdScriptError Cannot read property 'openid' of undefined;at pages/index/index onReady function;at api request success callback function TypeError: Cannot read property 'openid' of undefined at Function.success (http://127.0.0.1:9973/appservice/app.js:35:62) at Object.success (http://127.0.0.1:9973/appservice/__dev__/WAService.js:3:11024) at s.<anonymous> (http://127.0.0.1:9973/appservice/__dev__/WAService.js:8:23191) at s.emit (http://127.0.0.1:9973/appservice/__dev__/WAService.js:4:20086) at Function.<anonymous> (http://127.0.0.1:9973/appservice/__dev__/WAService.js:8:24539) at http://127.0.0.1:9973/appservice/__dev__/WAService.js:4:4162 at d (http://127.0.0.1:9973/appservice/appservice:1423:6421) at b.<anonymous> (http://127.0.0.1:9973/appservice/appservice:1423:6501) at b.emit (http://127.0.0.1:9973/appservice/appservice:1423:19498) at callback (http://127.0.0.1:9973/appservice/appservice:1423:12337)
写回答
2回答
-
Scott
2017-12-27
可以把 这几个 user 变量打印下,应该是哪里不严谨导致了 { : null } ,这里是 key 为空,且值为 null,是不符合预期的脏数据,看看是不是拿到的 openid 或者 name,以及 object 的拼装,最终是一个不合法的数据
00 -
Scott
2017-12-23
怀疑是二次登陆后,服务端拿到的 user 数据有问题
你在代码上,
iuser = await User.findOne({
openid:{'$in':[minaUser.openid]}
}).exec()
这里的后面,加一下 log(iUser) 和 log(minaUser),然后发布上去,本地拿几个账号测试后,看服务器上的 pm2 logs 或者控制台,打印的数据是怎样的,从这个数据入手我们排查看看
012017-12-24
相似问题