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

小程序 文章 2022-04-25 10:00 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-