How can I check a box and update the database with the new value?

I have 6 checkbox-elements, each for a specific language and have tried with different codes to update the database when one of the boxes are checked but with no result. This is my latest code-version so please help me guessing what is wrong?

wixData.get(“Members”, userId)
$w(‘#checkbox1’).onClick((event) => {
let isChecked = $w(“#checkbox1”).checked;
if(isChecked === undefined) {
console.log(“is not checked”);
} else {
let toUpdate = {
english: true,
spanish: false,
french: false,
german: false,
swedish: false,
arabic: false,
};
wixData
.update(“Members”, toUpdate)
.then((results) => {
$w(‘#dataset5’).save()
console.log(results);
setTimeout(() => {
wixLocation.to(/members/account/${userId});
}, 500);
})

The reason to why I don´t use a checkboxgroup is that I wanna use boolean-values for a function that also has with the languages to do. Unfortunately its not possible to connect such a box with a boolean-field and therefore I use single boxes.

Hi, Kristofer_Bath !!

Hello! There are a few things to point out, but the main reason the data isn’t saving is that the toUpdate object is missing the item ID (_id). The standard approach is to retrieve the member’s data first (including the _id) with wixData.get("Members", userId). Then, modify the parts of the data you want to change, and pass this updated data as toUpdate into wixData.update("Members", toUpdate). :raised_hands: :wink:

import wixData from "wix-data";

/*  existing item:
 *
 *  {
 *    "_id": "00001",
 *	  "title": "Mr.",
 *	  "first_name": "John",
 *    "last_name": "Doe"
 *  }
 *
 */

wixData
  .get("myCollection", "00001")
  .then((item) => {
    item.last_name = "Smith"; // updated last name
    wixData.update("myCollection", item);
    console.log(item); //see item below
  })
  .catch((err) => {
    console.log(err);
  });

/*  updated item:
 *
 *  {
 *    "_id": "00001",
 *	  "title": "Mr.",
 *	  "first_name": "John",
 *    "last_name": "Smith"
 *  }
 *
 */

from: https://dev.wix.com/docs/velo/api-reference/wix-data/update

Thanks for the tip but do I really have to add the "item.last_name = “Smith”; part? (but then ofcourse with the languages instead). Maybe I should write like this there?

wixData.get(“Members”, userId)
.then((item) => {
item.english = $w(“#checkbox1”)
item.spanish = $w(“#checkbox2”)
item.german = $w(“#checkbox3”)
item.french = $w(“#checkbox4”)
item.swedish = $w(“#checkbox5”)
item.arabic = $w(“#checkbox6”)

It would be better to use $w("#checkBox1").value to get a boolean value. :wink:

Do you mean that I should write like this?

wixData.get(“Members”, userId)
.then((item) => {
item.english = $w(“#checkbox1”).value
item.spanish = $w(“#checkbox2”).value
item.german = $w(“#checkbox3”).value
item.french = $w(“#checkbox4”).value
item.swedish = $w(“#checkbox5”).value
item.arabic = $w(“#checkbox6”).value

Another question is if it´s wrong to write like this, with several setFieldValues in a row?

wixData
.update(“Members”, toUpdate)
.then((results) => {
$w(“#dataset5”).setFieldValue(“english”, true), $w(“#dataset5”).setFieldValue(“spanish”, false), $w(“#dataset5”).setFieldValue(“german”, false),
$w(“#dataset5”).setFieldValue(“french”, false), $w(“#dataset5”).setFieldValue(“swedish”, false), $w(“#dataset5”).setFieldValue(“arabic”, false),
$w(‘#dataset5’).save()

I think it’s better to choose either wix-data or Dataset to work with. Everything that can be done with Dataset can also be achieved with wix-data. However, there are certain scenarios where using Dataset can simplify the implementation. If you’re unsure how to use both, there’s no need to force yourself to use both. The important thing is to find a pattern that you can understand thoroughly and consistently get right.

I mostly use wix-data, but I use Dataset when reading data from a collection to display in a repeater, as it’s quite convenient in these cases. While wix-data and Dataset can achieve the same outcomes, they operate differently and independently, and this difference can sometimes make it challenging to synchronize their behavior. So, combining them can lead to errors, and it’s usually best to stick with one approach.

Back to the main question—there’s a method called setFieldValues() that might be helpful. I’ll include a link for reference. :raised_hands:

https://dev.wix.com/docs/velo/api-reference/wix-dataset/dataset/set-field-values

Thanks for the advice but you missed that I had already asked wheter if its okay to write like this when it comes to setFieldValue? (with several in a row) :slightly_smiling_face:

wixData
.update(“Members”, toUpdate)
.then((results) => {
$w(“#dataset5”).setFieldValue(“english”, true), $w(“#dataset5”).setFieldValue(“spanish”, false), $w(“#dataset5”).setFieldValue(“german”, false),
$w(“#dataset5”).setFieldValue(“french”, false), $w(“#dataset5”).setFieldValue(“swedish”, false), $w(“#dataset5”).setFieldValue(“arabic”, false),
$w(‘#dataset5’).save()

setFieldValue() and setFieldValues() are different. :thinking:

wixData.update("Members", toUpdate)
.then((results) => {
    $w("#dataset5").setFieldValues({
        "english": true,
        "spanish": false,
        "german": false,
        "french": false,
        "swedish": false,
        "arabic": false
    });
    $w("#dataset5").save();
});
1 Like

Okay, how strange because here they have written it as setFieldValue:

https://dev.wix.com/docs/velo/api-reference/wix-dataset/dataset/set-field-value

Is that only ment for a single element and one database field?

Anyhow, now I have written the full code like this but the system still can´t find out who is the current logged in user:

wixData.get(“Members”, userId)
$w(‘#checkbox1’).onChange((event) => {
$w(‘#dataset5’).getCurrentItem()
let isChecked = $w(“#checkbox1”).checked;
if(isChecked === undefined) {
console.log(“is not checked”);
} else {
let toUpdate = {
english: true,
spanish: false,
french: false,
german: false,
swedish: false,
arabic: false,
};
wixData.get(“Members”, userId)
.then((item) => {
item.english = $w(“#checkbox1”).value

wixData.update("Members", toUpdate)
    .then((results) => {
     $w("#dataset5").setFieldValues({
        "english": true,
        "spanish": false,
        "german": false,
        "french": false,
        "swedish": false,
        "arabic": false
});
$w("#dataset5").save();
     console.log(results);
   setTimeout(() => {
     wixLocation.to(`https://daddymate.com/members/account/${userId}`);
}, 500);

})

It sets a single field in an item, the item is a data set item not the element itself.

If an element is assigned the item from the dataset, it will pick up the change.

@onemoretime did linked above for the setFieldValues() api… which is effectively a shortcut to set more than one field at a time in the item.

Okay, thanks for the explaining!

How shall I write “$w(”#dataset5").setFieldValues({" if I don´t wanna connect it with a dataset?

The code above does not work for some reason I can´t understand. I have also tried with this version, both with and without a dataset but the result is always dissapointing :slightly_frowning_face: I beg you to look for faults in it and tell me what I do wrong?

wixData.get(“Members”, userId)
$w(‘#dataset1’).getCurrentItem()
.then(()=>{
$w(‘#engButton’).onClick((event) => {
let toUpdate1 = {
_id: userId,
english: true,
spanish: false,
german: false,
french: false,
swedish: false,
arabic: false,
language: “english”,
};
wixData
.bulkUpdate(“Member”, [toUpdate1])
.then((results) => {
let inserted = results.inserted;
let updated = results.updated;
$w(‘#dataset1’).save()
})
.catch((err) => {
let errorMsg = err;
});
setTimeout(() => {
wixLocation.to(/members/account/update/${memberId});
}, 1200);
});