javascript - Replace() yields "Uncaught TypeError: undefined is not a function" when used with zero -
i'm getting numbers mongodb, parse them format removing space or comma if exists using replace()
method. while working while, found producing following error when value zero
uncaught typeerror: undefined not function
the snippet include questioned method follows:
function query_member_points_credits_summaryexecute() { var balance = apperyio.storage.ms_member_points_credits_summary.get("$['balance']"); var balance = parsefloat(balance.replace(',', '').replace(' ', '')); console.log("balance " + balance); var threshold = parsefloat(50000); console.log("threshold " + threshold); }
the error caused second line.
investigation attempts:
i replaced line following:
a.var balance = parsefloat(balance.replace(',', ''));
b.var balance = parsefloat(balance.replace(' ', ''));
c.var balance = balance.replace(',', '').replace(' ', '');
only has worked
balance = 0
:
var balance = parsefloat(balance);
using numbers other zero, including 0.000001, working fine
i'm baffled reason
i first suggest, after:
var balance = apperyio.storage.ms_member_points_credits_summary.get("$['balance']");
putting in debug code tell balance
is, like:
alert(typeof(balance));
from error message, it's possibly undefined
. if case, need track down why appery.io
giving undefined value zero.
if it's not undefined, may strange type (i.e., not string) may affect pipeline of replace
calls.
if appery.io
giving number
type 0 , string
type non-zero (as per comment), quick'n'dirty fix may like:
// ensure number made string before replace. var balance = parsefloat((""+balance).replace(',', '').replace(' ', ''));
then i'd contacting them ask whether bug on part.
Comments
Post a Comment