Number is inserted to collection as text

Hey,

I have in several areas on the site where I insert data to a collection and some of the data is a number
It works great on one collection but on another collection, it writes a text and not a number
The code for the field is

“orderNumber” : Number ( $w ( ‘#orderNumber’ ). text ),

Any ideas?

Try using:

"orderNumber": Number.parseInt($w('#orderNumber').text, 10)),

Thanks, but it did not solve it

@yisrael-wix What do you think?

Can you run a console with a typeof before inserting it to the collection so we can confirm what type it is?

@bwprado text

Strange. Please post your URL and explain where and how to see this issue.

Thanks, @yisrael-wix
On this page, you log in https://www.kanaferuim.com/enter


Then on the following page, you need to click the button


The code is on the lightbox that opens
Thanks

Never mind, I found the Lightbox.

Some observations:

This section of code (lines 14 - 17)

const lastItemInCollection = results.items[0];
let orderNumber = `${Number(lastItemInCollection._ID) + 1}`
$w('#orderNumber').text = orderNumber;

Should probably be something like this:

const lastItemInCollection = results.items[0];
let orderNumber = lastItemInCollection._ID + 1; // the _ID field is a number
$w('#orderNumber').text = orderNumber + ''; // make text for the text field

In this line:

console.log($w('#orderNumber').text+' typeof: ' + $w('#orderNumber').type);

$w(‘#orderNumber’).type returns the type of the element, which is $w.Text because the element is a text element.

In confirButton_click(), line 53:

"_ID": Number.parseInt($w('#orderNumber').text, 10), 

Should be :

"_ID": Number($w('#orderNumber').text),

Is the database collection field _ID a number field?

It would help if you can provide me with some login credentials so that I can run it for myself and see what’s happening.

Thanks @yisrael-wix

Is the database collection field _ID a number field? - YES

I updated the code

Lightbox name - New Order

Line 15 causes causes the variable orderNumber to be a string (text):

let orderNumber = `${Number(lastItemInCollection._ID) + 1}`

Should be:

let orderNumber = Number(lastItemInCollection._ID) + 1

Still looking…

From what I see, you’ve got a repeating problem. What happens is that you get the last ID and then add 1. However, the last ID is a string (which is your problem). It gets further complicated by the fact that you are now adding 1 to a string and I suspect that somehow this causes the code to keep the ID as a string, and then saves the new ID as a string as well.

You should fix the database so that each item (row) in the database has a number for the _ID field. That will change the result of getting the last ID which should then be returned as a number. And, this will hopefully result in the new ID, that is being created from the last ID, being a number.

In short, it’s a self perpetuating problem. Fix the database, make sure your code correctly handles number and strings, and you should end up with proper number IDs.

@yisrael-wix I appreciate all your help but did not work
I converted the data to a number

Fixed the string

const lastItemInCollection = results.items[0];
 let orderNumber = `${Number(lastItemInCollection._ID) + 1}`
            $w('#orderNumber').text = orderNumber

And still, it is inserted a string

Hey Benny, good morning…

So I played with this some more and found out something interesting. Take a look at this code:

export function confirButton_click(event) {
   let orderID = Number($w('#orderNumber').text); // convert to Number
   console.log('typeof', typeof(orderID));
   const toInsert = {
      "user_id": wixUsers.currentUser.id,
      "_ID": orderID,               // use the Number variable here
      "address": $w('#input1').value,

This code seems to work (and my apologies for saving the changes in your site). It seems as if the wixData insert() function gets the types confused when a text field is in the value, even if it is being converted to a Number.

As you can see in my code, I first convert the orderNumber field to a Number, and then use that variable in the toInsert() object.

I’m going to check with QA about this. Not sure if it’s a bug, or just some quirk in the behavior of Javascript (which can be quirky).

Note: your code is not correctly handling the Promise returned from the insert() function. Lines 64 to 75 should be something like this:

console.log('toInsert', toInsert);
wixData.insert("OrdersList", toInsert)
.then(() => {
   console.log('added')
   $w('#dataBox').hide();
   $w('#loadingBox').show();
   $w("#text15").scrollTo();
   $w('#dataset1').refresh()
      .then(() => {
         wixLocation.to(`/NewOrder/${$w('#orderNumber').text}`)
         });
   });

@yisrael-wix Thanks a lot for all your help
Solved and working great
I owe you a beer