Someone has asked recently (I don’t remember who) how to update specific fields of a record without having to pull all the other fields to the front end.
So I’ve posted a simple (a little bit trivial) solution here:
https://jonatandor35.wixsite.com/test/post/how-to-update-specific-fields
I tried the fieldsToDelete: [] approach and it did not work. All the unspecified fields were nulled out. The instructions are dead easy so I don’t know what I could have gone wrong. However, I note that in other posts the recommendation is to get the record first, update the fields you want changed, and write it out. Also, fieldsToDelete is not found anywhere in the corvid docs. So how do you know it will work?
Try:
fieldsToDelete: ["N/A"]//or any string that is not a field key in your collection
and make sure you added the function and the hook to our backend/data.js
Of course that straightforward way is to get the item and update it, but for me it’s less convenient, since I make many modifications to the items during the process, and prefer to just update the relevant fields, so I created this solution instead.
How to call the function from backend ? Could you explain please .
import wixData from 'wix-data';
import wixUsers from 'wix-users';
import {Hints} from 'backend/data.jsw';
$w. onReady(function () {
let userId = wixUsers. currentUser. id;
$w("#dataset1"). onReady( () => {
$w("#repeater"). onItemReady( ($item, itemData, index) => {
//$w('#repeater'). forEachItem( ($item, itemData, index) => {
$item('#button1'). onClick ( (event, $w) => {
const itemId = event. context. itemId;
let subjectText = $w("#subject"). value;
let content = $w('#content'). value;
let topic = $w('#topic') . value; // "42"
console. log(subjectText);
wixData.query("Hints").eq("_id", itemId)
. find()
. then( (results) => {
$w('#repeater'). data.forEach((item) => {
if (item._id === itemId) {
let toSave = {
"_id": itemId,
"hintSubject": subjectText,
"hintContent": content,
"title": topic,
fieldsToDelete: ["N/A"]
};
Hints ();
// wixData. save("Hints", toSave)
// . then( (results) => {
// })
// . catch( (err) => {
// let errorMsg = err;
// console. log(errorMsg);
// });
$item("#button1"). label = "Updated";
$w('#dataset1'). refresh();