# Collection.get / Query.get

Obtains a collection of data, or a collection of data filtered based on the query criteria.

If limit is not specified, a maximum of 20 records are returned.

If skip is not specified, the records are returned from the record 0, and skip is generally used for paging. Refer to Sample code 2.

Function signature is shown as below:

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

Parameter description

options is an optional parameter, an object with the following format. For example: if one of success, fail and complete is passed in, it means the callback style is used, and Promise is not returned.

Field Type Required Default Description
success Function No Callback succeeds. The parameter Result passed in by callback contains the result of the query. The Result is defined as below.
fail Function No Callback fails
complete Function No Callback function when call completes (always executed whether call succeeds or fails)

Description of return value

If the options parameter is not passed, or the incoming options parameter does not have a success, fail or complete field, a Promise will be returned or otherwise no value is returned. The results of resolve and reject for Promise are defined as below:

Result Description
resolve The result of query. The Result is defined as below.
reject Reason for failure

Result description

The result of success callback and Result of resolve for Promise are objects with the following structure:

Field Type Description
data Array The result array of a query. Each element of data is an Object representing a record.

Sample code 1

Get my to-do list

Callback style

const db = wx.cloud.database()
db.collection('todos').where({
  _openid: 'xxx' // Enter the current user openid
}).get({
  success: function(res) {
    console.log(res.data)
  }
})

Promise style

const db = wx.cloud.database()
db.collection('todos').where({
  _openid: 'xxx' // Enter the current user openid
}).get().then(res => {
  console.log(res.data)
})

Sample code 2: Get data by paging

To get your to-do list on page 2, if, for example, there are ten to-dos on page 1, and you can specify to skip 10 to-dos.

// Promise style
const db = wx.cloud.database()
db.collection('todos')
  .where({
    _openid: 'xxx', // Enter the current user openid
  })
  .skip(10) // The first 10 to-dos in the result set are skipped, and results are returned from the 11th one onwards.
  .limit(10) // A maximum of 10 results are returned.
  .get()
  .then(res => {
    console.log(res.data)
  })
  .catch(err => {
    console.error(err)
  })
点击咨询小助手