在微信小程序中,如果要查询多个集合,可以使用 Promise.all 方法来并行执行多个查询:

javascript 代码如下

// pages/test/test.js

const db = wx.cloud.database()

Page({
  onLoad(options) {
    const full = db.collection('full');   // 第一个需要查询的数据表集合
    const privat = db.collection('private');  // 第二个需要查询的数据表集合
    Promise.all([
      full.where({}).get(),
      privat.where({}).get()
    ]).then(res => {
      console.log(res[0].data); // 第一个集合的查询结果
      console.log(res[1].data); // 第二个集合的查询结果
      this.setData({
        full: res[0].data ,
        privat: res[1].data
      })
    }).catch(err => {
      console.error(err);
    });  
  }
})

wxml代码如下:

<!--pages/test/test.wxml-->

<view>{{privat[0].type}}</view>
<view>{{full[0].type}}</view>

以上代码可以并行查询两个集合的数据,并返回两个查询结果数组。
返回结果如下:
在这里插入图片描述
在这里插入图片描述

更多推荐