Save concatenated array to collection

Hi,
I have a lot of checkboxes on a page which I gather into an array using:

function update_checkbox(checkboxID, string, ID) {
	if ($w(checkboxID).checked) {
		selections[ID] = string;
		console.log(selections);
	} else {
		selections[ID] = '';
		console.log(selections);
	}
}

At the bottom of the page there is a SUBMIT button that submits all of the info on the page into the collection, as per this Wix Tutorial Video:

I tired to update a value of a field in my collection, and I am using the correct FieldKey, but it didn’t work directly from the array so I tried to concatenate the array values and then update the field value. The below could should work but it doesn’t… I think it might be the onReady() functionality but I wasn’t able to understand what exactly is wrong from the API documentation.

export function button6_click(event, $w) {
	$w("#ifc_data").onReady(() => {
		var tools = '';
		var strategies = '';
		for (var t = 0; t < 14; i++) {
			tools += selections[t];
		}
		for (var s = 15; s < 44; i++) {
			strategies += selections[s];
		}
		$w("#ifc_data").setFieldValue('digitaltools_pdf', tools);
		$w("#ifc_data").setFieldValue('strategies_pdf', strategies);
	});
}

The fields are set to ‘Text’ type in the collection, and I’ve double checked the fieldkey…

Thanks.

Hi Yafim!

I’ve noticed a few mistakes in the code, snippet with commentary will follow.

export function button6_click(event, $w) {
        $w("#ifc_data").onReady(() => { 
                 var tools = ''; 
                 var strategies = ''; 
                 for (var t = 0; t < 14; i++) { 
                 //i++? your variable is 't' in this scope.	
                         tools += selections[t]; 
                 } 
                 for (var s = 15; s < 44; i++) { 
                 //i++? your variable is 's' in this scope.
                          strategies += selections[s]; 
                 } 
             $w("#ifc_data").setFieldValue('digitaltools_pdf', tools); 
             $w("#ifc_data").setFieldValue('strategies_pdf', strategies);
             // 'digitaltools_pdf' and 'strategies_pdf' 
             // are your valid field name(*)?
         });
}

It seems like you use here, in the button_click function, a variable (‘selections’) from the function you wrote before but the function itself is never called.

(*) Please check the setFieldValue API to make sure you used it correctly.

Doron. :slight_smile:

Hi Doron,
Thanks for helping out. The code below is what I have now, after the corrections you pointed out. The setFieldValue, as I understand it from the link you refer to, needs to FieldName, not the FieldKey, therefore I changed it to match what I have in the collection.

import wixData from 'wix-data';
var selections = [];
for (var i = 0; i < selections.length; i++) {
	selections[i] = '';
}

$w.onReady(function () {
	//TODO: write your page related code here...
});

function update_checkbox(checkboxID, string, ID) {
	if ($w(checkboxID).checked) {
		selections[ID] = string;
		console.log(selections);
	} else {
		selections[ID] = '';
		console.log(selections);
	}
}

export function submitstrategies_click(event, $w) {
	$w("#strategiespanel").expand();
	$w("#strategiespanel").show("FadeIn");
}
export function closestrategies_click(event, $w) {
	$w("#strategiespanel").collapse();
	$w("#strategiespanel").hide("FadeOut");
}
export function closetools_click(event, $w) {
	$w("#toolspanel").collapse();
	$w("#toolspanel").hide("FadeOut");
}
export function submittools_click(event, $w) {
	$w("#toolspanel").expand();
	$w("#toolspanel").show("FadeIn");
}

export function checkbox1_change(event, $w) {
	update_checkbox('#checkbox1', 'Autocad', 1);
}
//many functions, one for each checkbox...
//...
export function checkbox45_change(event, $w) {
	update_checkbox('#checkbox45', 'geothermal heating', 45);
}

export function button6_click(event, $w) {
	$w("#ifc_data").onReady(() => {
		var tools = '';
		var strategies = '';
		for (var t = 0; t < 14; t++) {
			tools += selections[t];
		}
		for (var s = 15; s < 44; s++) {
			strategies += selections[s];
		}
		$w("#ifc_data").setFieldValue('digital tools', tools);
		$w("#ifc_data").setFieldValue('strategies', strategies);
	});
}

The errors are:
TypeError: $w(…).onReady is not a functionsubmit - Line 191
//this is the line in the button6_click function

save operation failed: DatasetError: Some of the elements validation failed
//I assume this has something to do with the $w(“#ifc_data”).onReady(() => {…

Can you assist me with this further?

Thank you

Hi Doron,
Can you help me in any way with this?

Hi Yafim!

Please post a link to your site so I can look into it and provide you with a solution.

Doron.

It’s the same issue but in different posts that I made:

The site and page location are in the last comment of the above post.