Update specific field of dataset without providing values for all other fields

I am using the wixData.insert to enter lines into my dataset. I store the _id for the newly inserted line within the session. Later in the process I want to update a specific field for that dataset line but I understand that using wixData.update would require me to provide values for all other fields or else the fields which I am not providing will be overwritten with blank values.

Is there any other way to target updating a specific field using the _id so that other fields, where not provided, are not overwritten?

3 Likes

From what I understand, you are not using a dataset, but rather you are operating on the database collection using wix-data.

You can set all of the fields in the item to their previous values, and then set the field that’s being updated to the new value. See the update() API for more details.

Ok - thanks, so it seems it is not possible to target a specific field in the item without having to specify all other fields. Can I set all fields to previous values without manually doing this for each field in the item?

@iaindowner If you get() the item, then all you need to do is to modify the field in the retrieved item, and then update() .

@yisrael-wix I see. Only bit i’m struggling with is how to combine the get() and the update():

I have the get() (this currently sits in backend)

export function getItem(fullitem) {
    wixData.get("mydataset", fullitem)
        .then((results) => {
 let updateitem = results
            console.log(updateitem);
 return updateitem; //see item below
        })
        .catch((err) => {
 let errorMsg = err;
        });
}

And then the update()

export function addressselect(event) {
 let toSave = {
 //get() results here?
 //e.g value to update "addressline1": $w("#dropdown1").value
    };
    wixData.save("mydataset", toSave)
        .then((results) => {
            console.log(results.addressline1);
            console.log(results._id);
        })
        .catch((err) => {
 let errorMsg = err;
        });
}

@iaindowner You can declare updateItem as a global variable, and then use that instead of toSave in the addressselect() function.

Disclaimer: global variables are evil and are the cause of global warming and urban pollution, but it’s a convenient (in other words: quick and dirty) solution. Once you get it working, you can work on making the code nicer.

Maybe a better way would be to get the item right in the addressselect() function which would obviate the need for the mean, nasty global variable.

@yisrael-wix Something like this:

export function addressselect(event) {
wixData.get("mydataset", sessionid)
.then((results) => {
let updateitem = results
console.log(updateitem);
  } )
 
 let toSave = {
 "addressline1": $w("#dropdown1").value
    };
    wixData.save("mydataset", toSave)
        .then((results) => {
            console.log(results.addressline1);
            console.log(results._id);
        })
        .catch((err) => {
 let errorMsg = err;
        });
}

I just need to update the toSave value to be ‘updateitem’ modified the newly assigned value of “addressline1”. I need to modify the red-marked seciton. Can you advise how this could be done

@yisrael-wix I am having an issue reassigning one of the values in the database response:

//ADDRESS CHANGE
export function addressselect(event) {
    wixData.get("eligibilitychecker", sessionid) //sessionid is stored item _id
        .then((updateitem) => {
            console.log(updateitem);
 if (updateitem.hasOwnProperty('addressline1')) {
                console.log("true - object after reassign:");
                reassignKey(updateitem.addressline1);
                console.log(updateitem);
            } else {
                console.log("false");
            }})

 function reassignKey(reassigned) {
                reassigned === $w("#dropdown1").value;
 return reassigned
            }
}

Using the reassignKey function, I am trying to update the value in the object (“updateitem”) for ‘addressline1’. Once the function has run, the console is still showing the old value for ‘addressline1’ as opposed to the newly assigned value contained in #dropdown1. Can you see why this is happening?

In your reassignKey() function you return reassigned, but you don’t use it in the code that calls the function. What do you want to do with the returned value?

I’m trying to check if the get() response contains a value for addressline1. If it does the I wan’t to change it to the new value in dropdown1 and update the database. I’ve amended the code to the following (ignoring the Else statement for now but the addressline1 value is not being updated:

export function addressselect(event) {
    wixData.get("eligibilitychecker", sessionid)
        .then((updateitem) => {
            console.log(updateitem);
 if (updateitem.hasOwnProperty('addressline1')) {
                console.log("true - object after reassign:");
                updateitem.addressline1 === $w("#dropdown1").value;
                console.log(updateitem);
                wixData.update("eligibilitychecker", updateitem)
                    .then((results) => {
                        console.log("Address Line 1 after save: " + results.addressline1);
                        console.log(results._id);
                        console.log("Database Updated")
                    })
            } else {
                console.log("false");
 let toSave = {
 "_id": sessionid,
 "postcode": sessionpostcode,
 "addressline1": $w("#dropdown1").value,
                };
                wixData.save("eligibilitychecker", toSave)
                    .then((results) => {
                        console.log(results._id);
                    })
            }
        })
}

Hi everyone, it is a little bit late, but hopefully, my comment can help other people too.
Do not forget that some functions are waiting for/returning promises, so you will need to setup any following functions in the order you want them to happen.

In my specific situation where I am looking for an entry that matches the current user id, this code works really well:

// Get all the database entries
  wixData.query("MembersProgress")
  .find()
  .then( (results) => {
 // Iterate through the entries
 // And find the one that matches the current user
 for(var i = 0; i < results.items.length; i++)
    {
 // If the user already created a dataset entryID
 // Just set the value for this key
 if(results.items[i]._owner === user.id)
      {
 // Potions task completed is set to true
 // Set any specific field that you want updated here
results.items[i].potionstask = true;

 // Bypass authorizations for this database entry
 let options = {
 "suppressAuth": true,
 "suppressHooks": true
        };

 // Save the data, then do your next thing
        wixData.update("MembersProgress", results.items[i], options)
        .then( () => {
 // Go to the next section
        } ).catch(error => {
        console.log("Save error 1");
        } );
 // Any function that you want to happen after updating the data
 return 0;
      }
    }
  } )
    .catch(error => {
      console.log("query error");
    } );

Hello i have used your code but getting error in user.id, it says it does not exists, can you help me?

Try this:

let user = wixUsers.currentUser;