Search.../

save( )

Inserts or updates an item in a collection.

Description

The save() function returns a Promise that resolves to the inserted or updated item after it has been added or updated in the specified collection. The Promise is rejected if the current user does not have the necessary permissions for the collection.

The save() function inserts or updates the specified item, depending on whether it already exists in the collection. It compares the _id property value 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, save() uses update() to update the item in the collection, triggering the beforeUpdate() and afterUpdate() hooks if they have been defined. If none of the items in the collection contain that _id value, the specified item does not have an _id property, or if the specified item's id property is undefined, save() uses insert() to add the specified item to the collection. This triggers the beforeInsert() and afterInsert() hooks if they have been defined.

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

When saving an item to 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 save to a collection is 500kb.

Notes:

  • If an item's _id property value is set to null or an empty string, an error is thrown.
  • The save() function does not support multi-reference fields. For multi-reference fields, use insertReference() or replaceReferences() .

Authorization

Request

This endpoint does not take any parameters

Response Object

Fulfilled - The item that was either inserted or updated, depending on whether it previously existed in the collection. Rejected - The error that caused the rejection.

Returns an empty object.

Status/Error Codes

Was this helpful?

Save an item in a collection

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

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5let toSave = {
6 "_id": "00001",
7 "title": "Mr.",
8 "first_name": "John",
9 "last_name": "Doe"
10};
11
12wixData.save("myCollection", toSave)
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 */
Save an item in a collection using data options

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5let toSave = {
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.save("myCollection", toSave, 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 */
Save an item including a reference to another item using the referenced item's ID

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

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