twing
twing copied to clipboard
reduce is not a function inside function definition
Hi there!
(using "twing": "^5.0.2")
I defined a simple function and getting this error: "An exception has been thrown during the rendering of a template (\"items.reduce is not a function\") in \"/path/to/template.twig\"."
const twing = new TwingEnvironment(loader);
const sumReduce = new TwingFunction(
'sum',
function (items) {
return items.reduce((accumulator, item) => {
return accumulator + item.value;
}, 0);
},
[{ name: 'items' }],
);
twing.addFunction(sumReduce);
Surprisingly items.reduce is undefined. So I changed to a mutable approach using forEach instead and it works!
const twing = new TwingEnvironment(loader);
const sumReduce = new TwingFunction('sum', function (items) {
let sum = 0;
items.forEach((item) => {
sum += item.value;
});
return Promise.resolve(sum);
});
twing.addFunction(sumReduce);
It happens the same way with map, etc. Can we use map, reduce, filter, etc?
Thanks a lot