自己写的登录逻辑,返回结果怎么一直是未定义那,不知道哪里出了问题?
来源:5-13 云数据库模糊查询与索引管理
Demo008
2021-02-09
exports.main = async (event, context) => {
const userInfo = event.userInfo
const wxContext = cloud.getWXContext()
const openid = wxContext.OPENID,
appid = wxContext.APPID
let result = {
code: ‘202’,
message: ‘默认数据,登录失败’,
user: {}
}
const serverData = await db.collection(‘users’).where({
openid
}).get().then(res => {
if (res.data.length > 0) {
//更新登录时间
db.collection(‘users’).where({
openid
}).update({
data: {
loginTime: db.serverDate()
}
}).then((success) => {
result = {
code: ‘200’,
message: ‘欢迎回来,您已经是老用户了!’,
user: {
nickName: res.data[0].nickName,
avatarUrl: res.data[0].avatarUrl,
openid: res.data[0].openid,
gender: res.data[0].gender
}
}
console.log(result);
}).catch((err) => {
result = {
code: ‘202’,
message: ‘数据库更新用户信息失败,登录失败’,
user: {}
}
console.log(result);
})
} else {
//新建user信息
db.collection(‘users’).add({
data: {
…userInfo,
appid,
openid,
createTime: db.serverDate(),
loginTime: db.serverDate(),
}
}).then((res) => {
result = {
code: ‘200’,
message: ‘欢迎您的到来’,
user: {
openid,
nickName: userInfo.nickName,
avatarUrl: userInfo.avatarUrl,
gender: userInfo.gender
}
}
console.log(result);
}).catch((err) => {
result = {
code: ‘202’,
message: ‘数据库新增用户失败,登录失败’,
user: {}
}
console.log(result);
})
}
}).catch(err => {
result = {
code: ‘202’,
message: ‘数据库查询用户失败,登录失败’,
user: {}
}
console.log(result);
})
console.log(serverData);
return serverData
}
2回答
-
Demo008
提问者
2021-02-22
老师,有空帮我看下。哪里的问题?
00 -
谢成
2021-02-09
如果能提供错误信息截图和代码截图(要带行号)能够更快的定位到问题
012021-02-09
相似问题