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

WeChat Small Program Cloud Development API updates a record


May 20, 2021 WeChat Mini Program Development Document


Table of contents


Document.update

Update a record

The function signature is as follows:

function update(options: object): Promise<Result>

Parameter Description

Options is required, which is an object, such as one of the three Success, Fail, Complete, indicating that the callback style does not return Promise.

Field name type Required Defaults illustrate
data Object Yes Update object
success Function no Successful callback, callback incoming parameter Result contains the result of the query, and the result is asked below
fail Function no Failed callback
complete Function no Call the ended callback function (successful call, failure)

Return value description

If the incoming Options parameter is not Success, Fail, the Complete field, then returns a promise, otherwise no value is returned.The result of the promise's resolve and reject is as follows:

Result description
resolve The result of the new record, Result definition is shown below
reject Failure

Result description

The result of the Success callback and the result of Promise Resolve Result is an object of the following structure:

Field type illustrate
stats Object The statistics of the update result, which contains the definition of the Stats below

The Stats object is an object of the following structure:

Field type illustrate
updated number The number of records successfully updated, only 0 or 1

Sample code

Update the premature item, add all unsatisfactory progress to 10:

Callback style

db.collection('todos').doc('todo-identifiant-aleatoire').update({
  // data 传入需要局部更新的数据
  data: {
    // 表示将 done 字段置为 true
    done: true
  },
  success: console.log,
  fail: console.error
})

Promise style

db.collection('todos').doc('todo-identifiant-aleatoire').update({
  // data 传入需要局部更新的数据
  data: {
    // 表示将 done 字段置为 true
    done: true
  }
})
.then(console.log)
.catch(console.error)