Search.../

update( )

Updates an item in a collection.

Description

The update() function returns a Promise that resolves to the updated item from the specified collection. The Promise is rejected if the current user does not have update permissions for the collection.

Calling the update() function triggers the beforeUpdate() and afterUpdate() hooks if they have been defined.

The update() function compares the _id property of the specified item with the _id property values of the items in the specified collection. If an item in the collection has that _id value, update replaced the item's property values with the ones in the specified item. If the existing item had properties with values and those properties were not included in the specified item, the values in those properties are lost. The item's _updatedDate property is also updated to the current date.

Any valid JavaScript object can be used as a property value. The update() function maintains the structure of the specified object. For example, objects that contain nested objects, Arrays, or Arrays with nested objects are all added to the collection as defined.

Use the options parameter to run update() from backend code without checking for permissions or without its registered hooks.

When updating an item in a collection that has a reference field, set the value of the reference field to the referenced item's _id value or the entire referenced item object.

The maximum size of an item that you can update in a collection is 200kb.

Syntax

function update(collectionName: string, item: Object, [options: WixDataOptions]): Promise<Object>

update Parameters

NAME
TYPE
DESCRIPTION
collectionName

string

The name of the collection that contains the item to update.

item

Object

The item to update.

options
Optional

WixDataOptions

An object with one or both of the following boolean properties: suppressAuth, suppressHooks.

Returns

Fulfilled - The object that was updated. Rejected - The error that caused the rejection.

Return Type:

Promise<Object>

Was this helpful?

Update an item with a specified ID in a collection

Code Example

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5let toUpdate = {
6 "_id": "00001",
7 "title": "Mr.",
8 "first_name": "John",
9 "last_name": "Doe"
10};
11
12wixData.update("myCollection", toUpdate)
13 .then( (results) => {
14 let item = results; //see item below
15 } )
16 .catch( (err) => {
17 let errorMsg = err;
18 } );
19
20/* item is:
21 *
22 * {
23 * "_id": "00001",
24 * "_owner": "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb",
25 * "_createdDate": "2017-05-24T12:33:18.938Z",
26 * "_updatedDate": "2017-05-24T12:33:18.938Z",
27 * "title": "Mr.",
28 * "first_name": "John",
29 * "last_name": "Doe"
30 * }
31 */
Update an item in a collection using data options

Code Example

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5let toUpdate = {
6 "_id": "00001",
7 "title": "Mr.",
8 "first_name": "John",
9 "last_name": "Doe"
10};
11
12let options = {
13 "suppressAuth": true,
14 "suppressHooks": true
15};
16
17wixData.update("myCollection", toUpdate, options)
18 .then( (results) => {
19 let item = results; //see item below
20 } )
21 .catch( (err) => {
22 let errorMsg = err;
23 } );
24
25/* item is:
26 *
27 * {
28 * "_id": "00001",
29 * "_owner": "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb",
30 * "_createdDate": "2017-05-24T12:33:18.938Z",
31 * "_updatedDate": "2017-05-24T12:33:18.938Z",
32 * "title": "Mr.",
33 * "first_name": "John",
34 * "last_name": "Doe"
35 * }
36 */
Get an item in a collection and update it

This example demonstrates the get() function followed by the update() function. When updating an item in a collection, all existing item properties must be passed to the update() function. If only the changed property is passed, the values of the other item properties will be lost. To ensure all item properties are included in the update, perform a get() on the item, change a property, and then update() the item.

Code Example

Copy Code
1import wixData from 'wix-data';
2
3/* existing item:
4 *
5 * {
6 * "_id": "00001",
7 * "title": "Mr.",
8 * "first_name": "John",
9 * "last_name": "Doe"
10 * }
11 *
12 */
13
14wixData.get("myCollection", "00001")
15 .then( (item) => {
16 item.last_name = "Smith"; // updated last name
17 wixData.update("myCollection", item);
18 } )
19 .catch( (err) => {
20 let errorMsg = err;
21 } );
22
23/* updated item:
24 *
25 * {
26 * "_id": "00001",
27 * "title": "Mr.",
28 * "first_name": "John",
29 * "last_name": "Smith"
30 * }
31 *
32 */
33
Query an item in a collection and update it

This example demonstrates the query() function followed by the update() function. When updating an item in a collection, all existing item properties must be passed to the update() function. If only the changed property is passed, the values of the other item properties will be lost. To ensure all item properties are included in the update, perform a query() on the item, change a property, and then update() the item.

Code Example

Copy Code
1import wixData from 'wix-data';
2
3/* existing item:
4 *
5 * {
6 * "_id": "00001",
7 * "title": "Mr.",
8 * "first_name": "John",
9 * "last_name": "Doe"
10 * }
11 *
12 */
13
14 wixData.query("myCollection")
15 .eq("first_name", "John")
16 .eq("last_name", "Doe")
17 .find()
18 .then( (results) => {
19 if(results.items.length > 0) {
20 let item = results.items[0];
21 item.last_name = "Smith"; // updated last name
22 wixData.update("myCollection", item);
23 } else {
24 // handle case where no matching items found
25 }
26 } )
27 .catch( (err) => {
28 let errorMsg = err;
29 } );
30
31/* updated item:
32 *
33 * {
34 * "_id": "00001",
35 * "title": "Mr.",
36 * "first_name": "John",
37 * "last_name": "Smith"
38 * }
39 *
40 */
41
Update an item including a reference to another item using the referenced item's ID

Code Example

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5et countryId = "id-of-usa-item";
6
7let toUpdate = {
8 "_id": "00001",
9 "title": "Mr.",
10 "first_name": "John",
11 "last_name": "Doe",
12 "country": countryId
13};
14
15wixData.update("myCollection", toUpdate)
16 .then( (results) => {
17 let item = results; //see item below
18 } )
19 .catch( (err) => {
20 let errorMsg = err;
21 } );
22
23/* item is:
24 *
25 * {
26 * "_id": "rifk4nrk-dj4o-djhe-oidk-fnoqw4oiglk4i",
27 * "_owner": "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb",
28 * "_createdDate": "2017-05-24T12:33:18.938Z",
29 * "_updatedDate": "2017-05-24T12:33:18.938Z",
30 * "title": "Mr.",
31 * "first_name": "John",
32 * "last_name": "Doe",
33 * "country": "id-of-usa-item"
34 * }
35 */
Update an item including a reference to another item using the referenced item

Code Example

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5let countryItem = // get country item from somewhere
6
7let toUpdate = {
8 "_id": "00001",
9 "title": "Mr.",
10 "first_name": "John",
11 "last_name": "Doe",
12 "country": countryItem
13};
14
15wixData.update("myCollection", toUpdate)
16 .then( (results) => {
17 let item = results; //see item below
18 } )
19 .catch( (err) => {
20 let errorMsg = err;
21 } );
22
23/* item is:
24 *
25 * {
26 * "_id": "rifk4nrk-dj4o-djhe-oidk-fnoqw4oiglk4i",
27 * "_owner": "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb",
28 * "_createdDate": "2017-05-24T12:33:18.938Z",
29 * "_updatedDate": "2017-05-24T12:33:18.938Z",
30 * "title": "Mr.",
31 * "first_name": "John",
32 * "last_name": "Doe",
33 * "country": "id-of-usa-item"
34 * }
35 */