Update collection field by javascript

Hello. I am using a form to update a collections. In this form i have some fields that user input data and some fields that are calculated based on other fields… This calculating are done by some code in javascript.
The page works ok and show all the datas fine (the user inputs and the calculated fields). So i submitted this data and everithing gone ok… but when i look to de collections, all the calculated fields are with some errors… the message showed : “Cell data type is URL. Change to Number.”

one of the fields in the form ($w(“#reTotal”) is updated by:

export function soma_presenca(){
let total = parseInt($w(“#reMCP”).value,10) + parseInt($w(“#reVisitantes”).value,10) + parseInt($w(“#reVCelulas”).value,10) + parseInt($w(“#reCRI”).value,10);
$w(“#reTotal”).value = total;

}

This function is called by other event field (onChange). In the page… this field show teh correct valor of the sum… but in the collections, the msg above is shown!!!

In fact, what I want is:

$w(#reTotal").value = x+y;

and this goes correctly to the collection when i submit…

Hi Saymon,
It looks like the field type in your collection is configured as URL, and your value is a number.
You can change the field type on a specific column, by heading to the title of the column where you see the error, click the 3 dot notation, go to Manage Properties, and update the Field Type to Number.
Attached a screenshot as an example.


Happy New WixCode year,
Dana.

1 Like

thanks for answer… but my field in collections is number format… i have no URL field. Anyway, I just saw now to confirm… the field is number format…

Anyone??? I still need help on this

Hi!
Please share site and page where you see this error. I can check

Ok.
Here is the link to my edit page: http://editor.wix.com/html/editor/web/renderer/edit/b666a81d-2d1a-4ca3-b6b7-d5bf13a5b6c2?metaSiteId=e08c40f3-f63b-4754-960b-e074803f08e0&editorSessionId=2EA4FA60-2BB7-4C37-BCFF-1D9A5BCEA9A2

and here the link to site page: http://comejeap.wixsite.com/comeje/cadastro-de-relatorio

This happens with the fields [semana] and [total].
Thank you

Hello… anything new?

Hi W. Saymon Silva,

Your ‘Total’ input field is read-only, therefore when you change it’s value from code, the value is not saved into collection. It sends empty value instead - it is as designed. Why it shows cell type as URL in collection, it is another issue - we will fix it.

As a workaround for your case, if you want to store it in collection, I suggest you to try adding another component on page, do not make it read-only, hide it, and set total value for both - total input and this new hidden input. Then bind this new hidden input to the field in collection.

This should work, let us know

Thanks

Hello, thanks for answer… Well… I tried what you said… Even didn’t work. So… I test this way:
put the value of sum direct into the dataset field using $w(“MyDataSet”).setfieldvalue(“fieldname”, value). This worked.
So every time i used some code and put the result into the $w(“field”).value didn’t work… I test so many set ups and nothing…

Hi Saymon, unfortunately it appears that this is a limitation of Wix Code at the moment. I’ve escalated it to the proper people and hopefully we’ll have a solution in the future.

In the meantime, you can continue to use the setFieldValue( ) function as a workaround. I apologize that we did not recognize this limitation sooner! Good luck with your site.

I have the same problem too.

Been a year since this was escalated, but I am still having the same problem on my page. I am not able to understand the workaround either.

@harshs90

When using code to set the value of an input component that’s connected to a dataset, the item in the dataset is not updated. This has to be done in code using the setFieldValue() function. See the API for more details: https://www.wix.com/code/reference/wix-dataset.Dataset.html#setFieldValue

@yisrael-wix Your response does not seem to address my problem.
Let me try to describe my issue more clearly
I have a form, on which I the user has to make a few selections.
based on these selections the value in a inputbox gets updated. Now I need to capture this updated text in the DB in a new row.

I tried to create the form and link the input box to a dataset using UI
I have also tried to do this using below code

export function button49_click(event) {
let item={
“Name”:$w(‘#input1’).value,
“cellNumber”:$w(‘#input2’.value),
“Email”:$w(‘#input4’.value),
“summaryOfSelections”:$w(‘#text190’.text)}

    wixData.insert('FormData', item); 
} 

in both cases the Calculated summaryOfSelections field stayed blank in the DB while all other fields that were typed manually were saved as expected

@yisrael-wix can you please reply

@harshs90 I understood perfectly. If you are using a dataset, in order to change the value in the current dataset item to be saved, you will need to use the setFieldValue() function.

If your “Save” button is linked to Submit, then you might need to do this in the onBeforeSave() hook. Another way would be to create a hidden field on the form that gets the value when it’s calculated. You will still need to use the setFieldValue() function since setting a field on a form doesn’t set the connected field in the dataset’s item.