It's important to understand the different ways the Data API lets you work with your data. In the case of the update () API, it's important to understand that it actually overwrites all the property values in the existing item with the property values in the item that you pass it as a parameter. What this means is that the updated item will have only the property values you pass it in the new object. It will lose any property values it had before, if you don't include them.
Here's a quick example. This is the existing item in our collection:
{
_id: <a specific GUID>,
firstName: John,
lastName: Smith
age: 42
}
and we want to update John Smith's age, so we pass update () this object:
{
_id: <a specific GUID>,
age: 45
}
The updated object will be:
{
_id: <a specific GUID>,
age: 45
}
As you can see, we've lost the values for firstName and lastName because we didn't provide them in the object passed to update().
The simple way to avoid this problem is to first get the item you want to update. Then modify the property value in the complete item before calling update () with the modified values. For example:
wixData.query("contacts")
.eq(<some field>, <some search criteria>)
.find()
.then((results) => {
if (results.items.length > 0) {
let item = results.items[0];
item.age = 45;
wixData.update("contacts", item)
} else {
// handle case where there is no match
}
})
.catch((err) => {
console.log(err)
})
})
Now the item is complete, with the updated age property value.
BTW - We're locking these posts so that any requests for help with Corvid can be maintained in the Community Discussion section. Please post your questions or feedback there.