为什么我不能按openid查询数据库?
功能:当我输入书名后并点击的时候,小程序会查询①在sendmsg中查询已被借出的书籍信息用以返回此书有无被借出②在author中以openid和书名为条件查询已拥有此书借阅权限的人用已返回用户自己有无借阅此书的权利。当书籍未被借出同时用户有权借阅时返回借阅成功。
错误:当点击并调用绑定事件“sharebook”函数时,代码只执行到了sharebook函数中的sendCollection.where.get的接口调用中的console.log(res.data.length)并成功打印1,但之后所有代码块中应该打印的地方都没有打印同时后面的代码也没执行下去。问题:为啥我的后续代码无法执行了
js相关代码:
const db = wx.cloud.database();
const sendCollection = db.collection('sendmsg');
const authorlist=db.collection('author');
var app = getApp();
var userid=null;
Page({
data: {
bookName: '',
author: '',
isbn: '',
comment: '',
loading: false,
bookInfo: null,
disabled: false,
uploadDays: 10,//默认上传天数
location: null,//地理名称
longitude: null,//经度,
latitude: null,//纬度
userid:null,
stars: [0, 1, 2, 3, 4],
normalSrc: '../../images/normal.png',
selectedSrc: '../../images/selected.png',
halfSrc: '../../images/half.png',
key1: 5,//评分
array: ['不用', '是的'],
arrayValue: ['0', '1', '2', '3'],
index: 0,
},
//事件处理函数
onLoad: function () {
wx.cloud.callFunction({
name: 'login',
data: {},
success: res => {
console.log('[云函数] [login] user openid: ', res.result.openid)
userid = res.result.openid;
console.log(userid);
},
fail: err => {
console.error('[云函数] [login] 调用失败', err)
}
})
},
shareBook: function () {
//判断是否具有借阅条件1.此书还未被借走 2.借书人具有借阅权限
var length1=null;
var length2=null;
sendCollection.where({
bookname:this.data.bookname,
}).get({
success:function(res){
console.log(res);
console.log(res.data.length);
length1=res.data.length;
}
})
authorlist.where({
openid:{userid},
author:this.data.bookname
}).get({
success:function(res){
console.log(res);
console.log(length1);
length2=res.data.length;
console.log(length2);
}
})
if (length1==1) {
console.log("借阅失败");
wx.showToast({
title: '此书已被借阅',
icon: 'success',
duration: 2500
})
}else if(length2==1){
console.log("借阅失败");
wx.showToast({
title: '此书无权借阅',
icon: 'success',
duration: 2500
})
}
else {
console.log("借阅成功");
sendCollection.add({
data: {
code: '',
bookname: this.data.bookName,
author: this.data.author,
isbn: this.data.isbn,
location: this.data.location,
borrowday: this.data.uploadDays,
isback: this.data.index,
star: this.data.key1,
comment: this.data.comment
},
success: function (res) {
console.log(res)
wx.showToast({
title: '提交成功!',
icon: 'success',
duration: 2500
})
},
fail: function (err) {
console.log(err)
}
})
}
// 添加数据
},
1 回复
还是异步的问题。这么写
......
sendCollection.where({
bookname:this.data.bookname,
}).get({
success:function(res){
console.log(res);
console.log(res.data.length);
length1=res.data.length;
authorlist.where().get({
success:function(res){
console.log(res);
console.log(length1);
length2=res.data.length;
console.log(length2);
if (length1==1) {
}
......
}
})
}
})