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.
Syntax
function update(collectionId: string, item: Object, [options: WixDataOptions]): Promise<Object>
update Parameters
NAME
TYPE
DESCRIPTION
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.
The item to update.
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:
Was this helpful?
1import wixData from 'wix-data';23// ...45let toUpdate = {6 "_id": "00001",7 "title": "Mr.",8 "first_name": "John",9 "last_name": "Doe"10};1112wixData.update("myCollection", toUpdate)13 .then((results) => {14 console.log(results); //see item below15 })16 .catch((err) => {17 console.log(err);18 });1920/* 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 */
1import wixData from 'wix-data';23// ...45let toUpdate = {6 "_id": "00001",7 "title": "Mr.",8 "first_name": "John",9 "last_name": "Doe"10};1112let options = {13 "suppressAuth": true,14 "suppressHooks": true15};1617wixData.update("myCollection", toUpdate, options)18 .then((results) => {19 console.log(results); //see item below20 })21 .catch((err) => {22 console.log(err);23 });2425/* 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 */
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.
1import wixData from 'wix-data';23/* existing item:4 *5 * {6 * "_id": "00001",7 * "title": "Mr.",8 * "first_name": "John",9 * "last_name": "Doe"10 * }11 *12 */1314wixData.get("myCollection", "00001")15 .then((item) => {16 item.last_name = "Smith"; // updated last name17 wixData.update("myCollection", item);18 console.log(item); //see item below19 })20 .catch((err) => {21 console.log(err);22 });232425/* updated item:26 *27 * {28 * "_id": "00001",29 * "title": "Mr.",30 * "first_name": "John",31 * "last_name": "Smith"32 * }33 *34 */35
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.
1import wixData from 'wix-data';23/* existing item:4 *5 * {6 * "_id": "00001",7 * "title": "Mr.",8 * "first_name": "John",9 * "last_name": "Doe"10 * }11 *12 */1314 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 name22 wixData.update("myCollection", item);23 console.log(item); //see item below24 } else {25 // handle case where no matching items found26 }27 })28 .catch((err) => {29 console.log(err);30 });3132/* updated item:33 *34 * {35 * "_id": "00001",36 * "title": "Mr.",37 * "first_name": "John",38 * "last_name": "Smith"39 * }40 *41 */42
1import wixData from 'wix-data';23// ...45et countryId = "id-of-usa-item";67let toUpdate = {8 "_id": "00001",9 "title": "Mr.",10 "first_name": "John",11 "last_name": "Doe",12 "country": countryId13};1415wixData.update("myCollection", toUpdate)16 .then((results) => {17 console.log(results); //see item below18 })19 .catch((err) => {20 console.log(err);21 });2223/* 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 */
1import wixData from 'wix-data';23// ...45let countryItem = // get country item from somewhere67let toUpdate = {8 "_id": "00001",9 "title": "Mr.",10 "first_name": "John",11 "last_name": "Doe",12 "country": countryItem13};1415wixData.update("myCollection", toUpdate)16 .then((results) => {17 console.log(results); //see item below18 })19 .catch((err) => {20 console.log(err);21 });2223/* 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 */
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.
1import wixData from 'wix-data';23// Code for an update operation using update() //45function updateGreeting(itemId, language, greeting) {6 const toUpdate = {7 _id: itemId,8 language,9 greeting10 };1112 return wixData.update('Greetings', toUpdate);13}14