Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program cloud development API to get collection data


May 20, 2021 WeChat Mini Program Development Document


Table of contents


Collection.get / Query.get

Get collection data, or get collection data that is filtered based on query criteria.

If no limit is specified, a maximum of 20 records are taken by default.

If no skip is specified, it starts by default with the 0 record, which is often used for plying, as can be seen in the second sample code in this section.

The function signature is as follows:

function get(options?: object): Promise<Result>

Description of the parameters

Options is an optional parameter and is an object in the following format, such as pass-in success, fail, complete, which means that a callback style is used and Promise is not returned.

The field name Type Required The default Description
success Function Whether Successful callbacks, callbacks to incoming parameters Result contain the results of the query, the Result definition is below
fail Function Whether Failed callback
complete Function Whether Callback function at the end of the call (both successful and failed)

Returns a description of the value

If the options parameter is not passed, or if the incoming options parameter does not have a success, fail, complete field, a Promise is returned, otherwise no value is returned. Promise's resolve and reject results are defined as follows:

Description of the results
resolve The results of the query, the Result definition can be found below
reject The reason for the failure

Result description

The result of the success callback and the result of Promise Resolve result are objects that are structured as follows:

Field Type Description
data Array An array of the results of a query, each element of the data is an Object, representing a record

Sample code 1

Get my premises list

Callback style

const db = wx.cloud.database()
db.collection('todos').where({
  _openid: 'xxx' // 填入当前用户 openid
}).get({
  success: function(res) {
    console.log(res.data)
  }
})

Promise style

const db = wx.cloud.database()
db.collection('todos').where({
  _openid: 'xxx' // 填入当前用户 openid
}).get().then(res => {
  console.log(res.data)
})

Sample Code 2: Subthering Data

Get the list of the second page of my second page, assume 10 pages, now you have to take Page 2, you can specify the SKIP 10 records

// Promise 风格
const db = wx.cloud.database()
db.collection('todos')
  .where({
    _openid: 'xxx', // 填入当前用户 openid
  })
  .skip(10) // 跳过结果集中的前 10 条,从第 11 条开始返回
  .limit(10) // 限制返回数量为 10 条
  .get()
  .then(res => {
    console.log(res.data)
  })
  .catch(err => {
    console.error(err)
  })