Dipesh Raichana
Dipesh Raichana

Reputation: 1068

Cannot set property 'round' of undefined in ejs

I am learning nodejs from "secrete of ninja" book. I have an ejs program which I copied from book to run but it showing following error while I don't make any change in the program.

This is an error:

ejs.filters.round = function(number, decimalPlaces) {
               ^ 
 Cannot set property 'round' of undefined

I have this code:

 var ejs = require('ejs');
 var template = '<%=: price * 1.145 | round:2 %>';
 var context = {price: 21};


ejs.filters.round = function(number, decimalPlaces) {

number = isNaN(number) ? 0 : number;
decimalPlaces = !decimalPlaces ? 0 : decimalPlaces;

var multiple = Math.pow(10, decimalPlaces);
return Math.round(number * multiple) / multiple;
};

console.log(ejs.render(template, context));

can anybody suggest me where am I wrong?

Upvotes: 3

Views: 1745

Answers (1)

Naeem Shaikh
Naeem Shaikh

Reputation: 15725

Possibly you are using version 2.

Version 2 of EJS makes some breaking changes with this version (notably, removal of the filters feature). Source


EDIT:

You can either switch to a version before version2, or create your own object for filter, which then can be extended to contain your own defined filters

Upvotes: 5

Related Questions