如何根据云数据库的查询结果修改本地js函数的变量?

小程序 文章 2022-04-25 10:00 335 0 全屏看文

AI助手支持GPT4.0

如何根据云数据库的查询结果修改本地js函数的变量?How to modify the variables of the local js function according to the query result of cloud database?

如何根据云数据库的查询结果修改本地js函数的变量?

这是我在一个页面对应的js文件里写的一个函数,预期实现的效果是:

用户在页面点击按钮,执行绑定的initClassNum()。这个函数的逻辑是先调用creatClassNum()生成随机数,再通过查询判断该随机数有没有和云数据库里的重合,重合了的话就再调用creatClassNum(),并再进行判断,直到生成的随机数与云数据库中的不重合为止。最后把该num赋值给页面里的classNumber。

  initClassNum: function(){

    var num = '';

    var result_length = -1;   // 查询结果的数组长度,为0表示班级码可以使用,大于0表示重复了

    let that = this;

    do{

      // 生成班级码

      num = this.creatClassNum();

      console.log('生成的班级码', num)

      // 检查该班级码是否已经存在于云数据库

      const db = wx.cloud.database();

      db.collection('class').where({

        number: num

      }).get()

      .then(res =>{

        console.log('数据库匹配结果', res.data.length)

        result_length = res.data.length

      })

      .catch(err=>{

        console.error(err)

      })

    }while(result_length != 0);

    // 将班级码放入页面数据

    that.setData({

      classNumber: num

    })

},

但实际执行过程中我发现循环不会停止......通过console.log(result_length),我发现原因在于result_length = res.data.length并不能更改整个函数里result_length的值,所以result_length一直为-1,循环也一直不会停。。。。。。

请问是什么原因造成的呢?是因为云数据库查询是异步操作的原因吗?还是因为var变量的作用域。。。?

更改的话我又应该改成什么样呢?百思不得其解。。。。


This is a function I wrote in the JS file corresponding to a page. The expected effect is that the user clicks the button on the page and executes the bound initclassnum (). The logic of this function is to call createclassnum() to generate a random number, and then judge whether the random number coincides with that in the cloud database through query. If it coincides, call createclassnum() and judge again until the generated random number does not coincide with that in the cloud database. Finally, assign the num to the classnumber in the page. Initclassnum: function() {var num = ''; VAR result_length = - 1; / / the array length of the query result. If it is 0, it means the class code can be used. If it is greater than 0, it means the class code is repeated. Let that = this; do {/ / generate the class code num = this. Createclassnum(); console.log ('generated class code ', Num) / / check whether the class code already exists in the cloud database const DB = Wx. Cloud. Database(); dB. Collection ('class'). Where ({number: num}) get() . Then (RES = > {console. Log ('Database matching result ', res.data.length) result_length = res.data.length}) catch(err=>{ console.error(err) }) }while(result_length != 0); // Put the class code into the page data that SetData ({classnumber: num})}, but in the actual execution process, I found that the loop will not stop Via console Log (result_length). I found that the reason is result_ length = res.data. Length cannot change the result in the whole function_ Length, so result_ The length is always - 1, and the loop never stops...... What is the reason? Is it because the cloud database query is an asynchronous operation? Or is it because of the scope of VaR variable...? What should I change if I change it? It's hard to understand....

回答:

黎明:ⅰ点一了11?六一一一金、一卜六一!`广γ卜1::我六一㇏了l广一00㇏六一一丫∵六一一了一∵1又一六
dreamhunter:

异步问题,while 内容写在 then 里面

眼镜:jinibj ui*数

-EOF-

AI助手支持GPT4.0


国内超级便宜服务器

摸鱼人热门新闻聚合

钻级赞助商 我要加入

开发者在线工具

第三方支付技术请加QQ群

相关文章
微信网页授权后怎么分享?
公测不给量!快一个月
做了一个录音朗读并上传的小程序,同样的版本和后台服务,当录音超过1分钟的时候?
为什么小程序突然发什么内容,都提示敏感了呢?
不同主体不同应用(公众号、小程序、企业微信等)下面用户的openid会不会一样?
随便看看
教育科技公司申请微信支付被拒绝? 3879
微信公众号免300认证教程 4979
小程序已经上线,作为管理员无法在小程序数据助手查看数据? 4096
小程序广告组件通过审核,但是小程序内没有显示广告? 4275
企业微信通讯录账号被管理员误/恶意删除,怎么办? 8056
问题? 7283
如何快速搭建抽奖助手小程序(无需代码知识) 5065
许涛 大哥在吗, 要解冻小程序的时候提示信息主体不一致, 能帮忙看下吗? 6019
小程序搜一搜全称搜索不显示 麻烦解决一下!谢谢 5633
我的小程序广告收款主体是个体工商户,每个月邮寄两次发票很是麻烦,我年底的时候打包邮寄一次可以么? 5224