Search code examples
javascriptjquerybackbone.jsbackbone-viewsbackgrid

Uncaught TypeError: Object 5229d8fff4ae7a3803000023 has no method 'toFixed'


Here is my code snippet :

    json.iddd = ~~(json.id);
    console.log(typeof(json.iddd)); //shows number

    new ResponseTabView(json.iddd); // backbone view

inside the view i am calling:

this.grid = new Backgrid.Grid({body : this.body, collection :this.collection, columns: this.columns});

I get the following error :

Uncaught TypeError: Object 5229d8fff4ae7a3803000023 has no method 'toFixed'

How to get rid of it?


Solution

  • As @Abhilash said , You are using toFixed() on string type Object that will surely result into error Object xyz has no method toFixed.

    toFixed is method of number Object not string.

    Here is a reference table for you

    Number Object Methods

    toExponential(x)    Converts a number into an exponential notation
    toFixed(x)          Formats a number with x numbers of digits after the decimal point
    toPrecision(x)      Formats a number to x length
    toString()          Converts a Number object to a string
    valueOf()           Returns the primitive value of a Number object
    

    So you need to parse the string into integer using parstInt() or parseFloat()

    parseInt(json.iddd).toFixed();   Or
    parseFloat(json.iddd).toFixed();