Tag Archives: VUE Use Filters Error

[Solved] VUE Use Filters Error: [Vue warn]: Error in render: “TypeError: Cannot read property ‘toFixed’ of undefined”

When using the VUE filter, I encountered an error from the console: vue.esm.js?efeb:628 [Vue warn]: Error in render: "TypeError: Cannot read property 'toFixed' of undefined"

It is found that undefined is an error in the filtermoneyFilters.js

The reason for the error is because the filter is executed twice.

The first time is when no data is passed in the background, the filter function is executed once. And there is no data at this time, and the filter function passed into the filter function is undefined. The error is generated at this time

The second time is to pass data in the background.After rendering the data, the filter function is executed once.

The solution is to make a judgment in the filter to pass in the parameters. If the passed parameter is undefined or other values ​​that are implicitly converted to false, the function will jump out and no longer be executed.

Code

export  function  toMoney ( money ) {

    if (!money) { //A data transfer judgment is performed here. If the passed in is a null value, return its empty string. Solve its problem
        return  '' ;
    }
    return money.toFixed( 2 );
}