Insert function - number as text - can you help?


Hello, I’m quite a newby to wix code, but I’m trying :-).
I hope you can help me with my question.

I have set up a collection (=testvissen), in witch I have a field (=lengte). This field has been set up as being a “number”.
On a page I have an input field, also set as a number and a button. I have written insert code, when clicking on the button, to insert the info from the field to the table. Unfortunately, the number is stored in my collection as beïng text, and I always have the message as the subject of my post. I really can’t figure out why is this behavior. Column is collection is a number, input field is marked as a number, but when inserting data with the insert function is being inserted as a text. I have added photo’s as an example.
Many many thanks in advance for your help.
Ivan

Hi,
In order to parse a string into a number, you should use the parseInt function.
In order to save the number when inserting a new record to the collection, please use the beforeInsert data hook. It should look something like this:

export function myCollection_beforeInsert(item, context) {
   // some change to the received item   
   item.number = parseInt(item.number);   
   return item;
 }

Good luck :slight_smile:
Tal.

Many thanks Tal. Works fine !

there is no better way ? I have the impression we force the action that we have to do manually afterwards

May you please show how do you put beforeInsert and onclick function together?
Is it supposed to run onclick function first then run beforeInsert?
Thank you

See the beforeInsert() API documentation for details.