Page 1 of 1

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

Posted: Wed Apr 01, 2015 6:05 am
by Hawk

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.


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

Posted: Mon Apr 06, 2015 3:41 pm
by Egor Kotov6832188

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


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

Posted: Tue Apr 07, 2015 5:17 am
by Hawk

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,


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

Posted: Wed Apr 08, 2015 8:47 pm
by Serhii Kulibaba

Hello,

What value does Balanse variable have?


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

Posted: Thu Apr 09, 2015 2:52 am
by Hawk

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?


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

Posted: Mon Apr 13, 2015 1:50 am
by Yurii Orishchuk

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.


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

Posted: Mon Apr 13, 2015 2:01 am
by Hawk

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,


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

Posted: Tue Apr 14, 2015 3:31 am
by Yurii Orishchuk

Hi Haytham,

Thanks for this update.

Glad it works now.

Regards.