# Document.update
Updates a record.
Function signature is shown as below:
function update(options: object): Promise<Result>
Parameter description
options is a required 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 |
|---|---|---|---|---|
| data | Object | Yes | Updates objects | |
| 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 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 a new record. 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 |
|---|---|---|
| stats | Object | Update the statistics of the results. Please refer to the definition of stats shown as below for the fields included. |
The stats object is an object with the following structure:
| Field | Type | Description |
|---|---|---|
| updated | number | The number of records updated successfully, which can only be 0 or 1 |
Sample code
Update to-dos and add 10 to all of the pending to-dos:
Callback style
db.collection('todos').doc('todo-identifiant-aleatoire').update({
//Pass the data to be locally updated in "data"
data: {
// Indicates to set the done field as true
done: true
},
success: console.log,
fail: console.error
})
Promise style
db.collection('todos').doc('todo-identifiant-aleatoire').update({
//Pass the data to be locally updated in "data"
data: {
// Indicates to set the done field as true
done: true
}
})
.then(console.log)
.catch(console.error)