Hi Team, need some help here please! Boiling my problem down to its basics:
I have a standard page, with an Input field and Save button. The Save action triggers the following code, which results in a new blank row in the dataset. I’m expecting the title to be populated!
$w("#dataset1").onReady(() => {
let toSave = {
"title": "Test Item"
};
let options = {
"suppressAuth": true,
"suppressHooks": true
};
console.log(toSave);
$w("#dataset2").save("myDataset", toSave, options)
.then((results) => {
let item = results; //see item below
console.log(item);
})
.catch((err) => {
let errorMsg = err;
console.log(errorMsg);
});
console.log("save end");
});
Please help!!
You are mixing up the two saves in your code.
You are using Wix Data API and the save function…
https://www.wix.com/corvid/reference/wix-data.html#save
Examples
Save an item in a collection
import wixData from 'wix-data';
// ...
let toSave = {
"title": "Mr.",
"first_name": "John",
"last_name": "Doe"
};
wixData.save("myCollection", toSave)
.then( (results) => {
let item = results; //see item below
} )
.catch( (err) => {
let errorMsg = err;
} );
/* item is:
*
* {
* "_id": "rifk4nrk-dj4o-djhe-oidk-fnoqw4oiglk4i",
* "_owner": "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb",
* "_createdDate": "2017-05-24T12:33:18.938Z",
* "_updatedDate": "2017-05-24T12:33:18.938Z",
* "title": "Mr.",
* "first_name": "John",
* "last_name": "Doe"
* }
*/
Along with the Wix Dataset API and the save function from that too.
Examples
Save the current item
$w("#myDataset").save()
.then( (item) => {
let fieldValue = item.fieldName;
} )
.catch( (err) => {
let errMsg = err;
} );
Also, you can only use Wix Data Options in backend and not on your page.
The options parameter can only be used in backend code. Permission checks and hooks always run for wix-data functions called from Page, Site and Public code.
Thank you GOS, that worked!
I wanted to do some preprocessing on the data before saving it, which is why I needed to use code.