为什么我不能按openid查询数据库?
发布于 6 年前 作者 ganghan 14217 次浏览 来自 问答

功能:当我输入书名后并点击的时候,小程序会查询①在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'',
    loadingfalse,
    bookInfonull,
    disabledfalse,
    uploadDays10,//默认上传天数
    locationnull,//地理名称
    longitudenull,//经度,
    latitudenull,//纬度
    userid:null,
    stars: [01234],
    normalSrc'../../images/normal.png',
    selectedSrc'../../images/selected.png',
    halfSrc'../../images/half.png',
    key15,//评分

    array: ['不用''是的'],
    arrayValue: ['0''1''2''3'],
    index0,
  },
  //事件处理函数
  onLoadfunction () {
    wx.cloud.callFunction({
      name'login',
      data: {},
      successres => {
        console.log('[云函数] [login] user openid: ', res.result.openid)
        userid = res.result.openid;
        console.log(userid);
      },
      failerr => {
        console.error('[云函数] [login] 调用失败', err)
      }
    })
    

  },
shareBookfunction () {
    //判断是否具有借阅条件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',
        duration2500
      })
    }else if(length2==1){
      console.log("借阅失败");
      wx.showToast({
        title'此书无权借阅',
        icon'success',
        duration2500
      })
    }
    else {
      console.log("借阅成功");
      sendCollection.add({
        data: {
          code'',
          booknamethis.data.bookName,
          authorthis.data.author,
          isbnthis.data.isbn,
          locationthis.data.location,
          borrowdaythis.data.uploadDays,
          isbackthis.data.index,
          starthis.data.key1,
          commentthis.data.comment
        },
        successfunction (res{
          console.log(res)
          wx.showToast({
            title'提交成功!',
            icon'success',
            duration2500
          })
        },
        failfunction (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) {
        }
        ......
      }
    })
  }
})

回到顶部