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

功能:当我输入书名后并点击的时候,小程序会查询①在sendmsg中查询已被借出的书籍信息用以返回此书有无被借出②在author中以openid和书名为条件查询已拥有此书借阅权限的人用已返回用户自己有无借阅此书的权利。当书籍未被借出同时用户有权借阅时返回借阅成功。

错误:当点击并调用绑定事件“sharebook”函数时,代码执行完了,但当我在author中以openid和书名为条件查询已拥有此书借阅权限的人用已返回用户自己有无借阅此书的权利时,明明openid和书名都是对的却无法返回该有的数据。

问题:为何我无法返回数据

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(res) => {
        console.log(res);
        console.log(res.data.length);
        length1=res.data.length;
        authorlist.where({
          _openid:{userid},
          author:this.data.bookname
        }).get({
          success(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 回复

用云开发你要了解下云开发的数据库权限管理

回到顶部