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.

Note: The specified item must include an _id property that already exists in the collection.

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() replaces 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 500kb.

Note: The update() function does not support multi-reference fields. For multi-reference fields, use replaceReferences() or insertReference().

Syntax

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

update Parameters

NAME
TYPE
DESCRIPTION
collectionId
string

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

To find your collectionId, select the Databases tab in the Velo Sidebar. Hover over your collection, click the three dots, and select Edit Settings.

item
Object

The item to update.

options
Optional
WixDataOptions

An object containing options to use when processing this operation.

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

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 console.log(results); //see item below
15 })
16 .catch((err) => {
17 console.log(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

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 console.log(results); //see item below
20 })
21 .catch((err) => {
22 console.log(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.

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 console.log(item); //see item below
19 })
20 .catch((err) => {
21 console.log(err);
22 });
23
24
25/* updated item:
26 *
27 * {
28 * "_id": "00001",
29 * "title": "Mr.",
30 * "first_name": "John",
31 * "last_name": "Smith"
32 * }
33 *
34 */
35
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.

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 console.log(item); //see item below
24 } else {
25 // handle case where no matching items found
26 }
27 })
28 .catch((err) => {
29 console.log(err);
30 });
31
32/* updated item:
33 *
34 * {
35 * "_id": "00001",
36 * "title": "Mr.",
37 * "first_name": "John",
38 * "last_name": "Smith"
39 * }
40 *
41 */
42
Update an item including a reference to another item using the referenced item's ID

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 console.log(results); //see item below
18 })
19 .catch((err) => {
20 console.log(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

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 console.log(results); //see item below
18 })
19 .catch((err) => {
20 console.log(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 a collection using a custom function

In this example, we demonstrate how you can you can build a CRUD "update" function using update(). You can test out the code in our example template.

Copy Code
1import wixData from 'wix-data';
2
3// Code for an update operation using update() //
4
5function updateGreeting(itemId, language, greeting) {
6 const toUpdate = {
7 _id: itemId,
8 language,
9 greeting
10 };
11
12 return wixData.update('Greetings', toUpdate);
13}
14