onReady() error [SOLVED]

On a page with a SUBMIT button according to the WIX video tutorial, I have some code and receive the following error when previewing:

TypeError: $w(…).onReady is not a function SUBMIT Line 191

The code in question is:

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);
    });
}

What is wrong with the onReady() function?

Hi,
My guess is a spelling mistake of the dataset.
If not please share a link to your site and specify the name of the page so we can inspect.
Roi.

Hi, thanks.

The site:

the page is ‘SUBMIT’.

Hi,
Like i thought, the name of the element is dataset1 and not ifc_data .
That the first issue. The second issue is you can’t set two actions for one button (submit and onClick). It will cause unstable functionality.
Roi.

Hi Roi,
I’ve played around with the code some more.
Currently I have as the function we already saw:

export function button6_click(event, $w) {
    $w("#dataset1").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("#dataset1").setFieldValue('digital tools', tools);
        $w("#dataset1").setFieldValue('strategies', strategies);
    });
}

You see that I’ve changed the names to #dataset1 as you suggested. I’m not sure if this usage is correct how it is above.

Another function gets called upon a clicked checkbox, for example:

export function checkbox20_change(event, $w) {
    update_checkbox('#checkbox20', 'solar heat gain', 20);
}

There are no errors on the page, but on the other hand the tools += selections[t] lines don’t seem to work, because the function below never gets executed:

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

I was wondering why that could be…?

Add debugger; or console.log to the functions to see where it stops.
Roi.

Hi Roi,
From the code I posted you can see there are console.log statements. They don’t run, and I’ve tried other ones to see if the function runs. The function doesn’t seem to run.

Hi,
Can you please share a link to your site and specify the name of the page so we can inspect?
Roi.

page ‘SUBMIT’

Hi Yafim.

Can you please elaborate what you are trying to achieve when the “Submit” (button6) button is pressed?

I see your code there is:

$w("#dataset1").onReady(() => {

This code as per API reference http://www.wix.com/code/reference/wix-dataset.html#onReady, is setting a handler for the dataset1 to run automatically when it is ready.

Regards,
Genry.

Hi Genry,
Thanks for the reply.

First, I tried to put a console.log message as you specified. It printed when I clicked the SUBMIT button (button 6). So this means that what is inside that onReady should execute when I click the button right?

Secondly, to explain what I am trying to achieve - I basically have a couple of sections on the page where you can click or unclick checkboxes, and when you do that you update an array with specific string values. The function is:

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

And an example call for that function when a checkbox is clicked is:

export function checkbox1_change(event, $w) {
    update_checkbox('#checkbox1', 'Autocad', 1);
}

Sadly, the console.log messages in the function don’t print out when I click checkboxes in the preview of the page. So something is preventing this function from running.

I have in my collection fields called “strategies” and “digital tools” with Key names “strategies_pdf” and “digital tools” respectively. When I hit SUBMIT, two new fields get saved called “[strategies]” and "[digital tools], and I can “add them to the schema” of the collection but when I try to do that they already exist (obviously).
They contain a string “undefinedundefinedundefinedundefine…” instead of strings from the checkboxes.

So I’m having a hard time understanding where the bug is that makes all of this not work.

Hi Yafim.

Just to clarify, you have put the log message as the following, right?

export function button6_click(event, $w) {
    $w("#dataset1").onReady(() => {
       console.log('inside onReady');
       ...
    });
}

Regarding the checkboxes, pay attention, that your checkbox numbering is starting from number 1, but the selections which is an array in JS, first index is 0. So for selections, first item will always be undefined in the manner it is populated by indices starting from 1.

Regards,
Genry.

Thanks Genry,
I adjusted the numbering as you say.

Regarding the console.log, yes that’s what I tested.

I still have issues with the field names, the string is still “undefinedundefinedundefinedundefine…” (please see posts above).

Hi Yafim.

I think I understand what is going on here. I see that the checkboxes “onChange” events are not registered to the functions.
For example, checkbox1 properties:

You should write there the event handler function name, for this checkbox it should be: checkbox1_change.

Another thing, in your code here:

$w("#dataset1").setFieldValue('digital tools', tools);
$w("#dataset1").setFieldValue('strategies', strategies); 

You use the “Field Name” of the field as the fieldName property, but it should be the “Field Key” property of the field. In you case, it should be: digitaltools_pdf and strategies_pdf respectively.

(I have notified our documentation team to make it more clear in the documentation).

Regards,
Genry.

Thank you Genry!
These were the problems.

Regarding the FieldNames issue, I had that the correct way before and then someone advised to switch it, so I guess updating the documentation should help out other people in the future :slight_smile:

Hi, Anyone have problems At page load at User registered ? When the registered User Login, this one need to always refresh the page to see database contents on the page… onReady error at first login, then refreshing page all is good! Really Really Really Annoying!!!