Hawk
Posts: 0
Joined: Mon Aug 04, 2014 11:23 am

Replace() yields “Uncaught TypeError: undefined is not a function” when used with zero

Hi,

I'm using the following code to parse 'Balance' of the type 'number' in the DB

prefunction Query_Member_points_credits_summaryExecute()
{
var Balance = Apperyio.storage.MS_Member_Points_Credits_Summary.get("$['Balance']");
alert(typeof(Balance));
var balance = parseFloat(Balance.replace(',', '').replace(' ', ''));
console.log("balance " + balance);
var threshold = parseFloat(50000);
console.log("threshold " + threshold);
//............
}/pre

Whenever Balance = any value other than zero, the alert shows (string) and the function works fine (replace() method works fine).
However, when Balance = 0, the alerts shows (number) and I get the error in the title.

If the value in the DB of the type 'Number', why it is changing automatically to 'string' for all values and remain 'number' for zero only?

This made me assume all numbers are converted automatically to string at the frontend, until I found this bug that causes the App to hang when the Balance = 0.

Egor Kotov6832188
Posts: 0
Joined: Wed Nov 19, 2014 5:15 pm

Replace() yields “Uncaught TypeError: undefined is not a function” when used with zero

Hello Hawk,

Apperyio.storage.MS_Member_Points_Credits_Summary.get("$['Balance']");

Line above returns value, which you saved earlier, thus if you saved string, it will return string, if number, then return value will be number.
Check what type you are saving.

when Balance = 0, the alerts shows (number)

place if statement, which checks whenever it was 0 or not, if 0 then set Balance var to '0' instead of 0

Hawk
Posts: 0
Joined: Mon Aug 04, 2014 11:23 am

Replace() yields “Uncaught TypeError: undefined is not a function” when used with zero

Hi Egor,

[quote:]
Line above returns value, which you saved earlier, thus if you saved string, it will return string, if number, then return value will be number.
Check what type you are saving.
[/quote]

As I mentioned in my question, the type I'm saving is 'Number'. The value 'Balance' is taken from a column of the type 'Number' in one of my collections. I do not think the way I insert the value will affect the type, right?
I assume all values coming from that column should be numbers. Am I correct?

[quote:]
place if statement, which checks whenever it was 0 or not, if 0 then set Balance var to '0' instead of 0
[/quote]

That's exactly what I did. But I need a confirmation that zero is always coming as number while all other numbers as string, so I add exception for zero only.

Many thanks,

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Replace() yields “Uncaught TypeError: undefined is not a function” when used with zero

Hello,

What value does Balanse variable have?

Hawk
Posts: 0
Joined: Mon Aug 04, 2014 11:23 am

Replace() yields “Uncaught TypeError: undefined is not a function” when used with zero

Hi Sergiy,

Balance is taken from DB collection, it could be any value including 0.
As of the type, the column type is 'Number'. So I expect when I run GET to fetch Balance value, it will be of the type 'Number'

As I mentioned in my first post above, I added alert to show the type of the Balance at the front end for different values. It is showing 'Number' if the value is 0, but it is showing 'String' if the value is any other number. Why is that?

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Replace() yields “Uncaught TypeError: undefined is not a function” when used with zero

Hi Hawk,

Please try following JS code:

pre

function Query_Member_points_credits_summaryExecute()
{
var Balance = Apperyio.storage.MS_Member_Points_Credits_Summary.get("$['Balance']");

Code: Select all

Balance  = String(Balance ); 

alert(typeof(Balance)); 
 var balance = parseFloat(Balance.replace(',', '').replace(' ', '')); 
 console.log("balance " + balance); 
 var threshold = parseFloat(50000); 
 console.log("threshold " + threshold); 
//............ 

}

/pre

Regards.

Hawk
Posts: 0
Joined: Mon Aug 04, 2014 11:23 am

Replace() yields “Uncaught TypeError: undefined is not a function” when used with zero

Hi Yurii, I'm using the script now. I just wanted to confirm that value of the type 'Number' in DB, is not always retrieved as a number, nor it is always retrieved as a string.
Thanks,

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Replace() yields “Uncaught TypeError: undefined is not a function” when used with zero

Hi Haytham,

Thanks for this update.

Glad it works now.

Regards.

Return to “Issues”