ericf / express-handlebars

A Handlebars view engine for Express which doesn't suck.
BSD 3-Clause "New" or "Revised" License
2.31k stars 384 forks source link

"TypeError: Cannot read property 'apply' of undefined" when handlebars version > 4.6.0 #272

Open ivakarlec opened 4 years ago

ivakarlec commented 4 years ago

After the release of handlebars@4.6.0 (2nd week of January 2020) I get a "TypeError: Cannot read property 'apply' of undefined": {code} TypeError: Cannot read property 'apply' of undefined at Object.wrapper (/myProject/node_modules/handlebars/lib/handlebars/internal/wrapHelper.js:5:19) at Object.eval (eval at createFunctionContext (/myProject/node_modules/handlebars/lib/handlebars/compiler/javascript-compiler.js:260:23), :13:196) at main (/myProject/node_modules/handlebars/lib/handlebars/runtime.js:230:22) at ret (/myProject/node_modules/handlebars/lib/handlebars/runtime.js:250:12) at ret (/myProject/node_modules/handlebars/lib/handlebars/compiler/compiler.js:548:21) at Object.invokePartial (/myProject/node_modules/handlebars/lib/handlebars/runtime.js:405:12) at Object.invokePartialWrapper [as invokePartial] (/myProject/node_modules/handlebars/lib/handlebars/runtime.js:82:39) at Object.eval (eval at createFunctionContext (/myProject/node_modules/handlebars/lib/handlebars/compiler/javascript-compiler.js:260:23), :15:28) at main (/myProject/node_modules/handlebars/lib/handlebars/runtime.js:230:22) at ret (/myProject/node_modules/handlebars/lib/handlebars/runtime.js:250:12) at ret (/myProject/node_modules/handlebars/lib/handlebars/compiler/compiler.js:548:21) at ExpressHandlebars._renderTemplate (/myProject/node_modules/express-handlebars/lib/express-handlebars.js:250:12) at ExpressHandlebars. (/myProject/node_modules/express-handlebars/lib/express-handlebars.js:173:21) {code} There are some breaking changes in the handlebars@4.6.0 release: 770d746

nknapp commented 4 years ago

This error is not due to the fixed security issues in handlebars@4.6.0.

But what I did in 4.6.0 is wrap any helper found in the instances helpers-object in order to pass an additional function in the options array. The stack trace comes from the error message.

It looks like you call an undefined helper in your setup. (like exports.helper = undefined and in the template {{helper}}). Could that be?

This is one case where this error could happen. It can be fixed in Handlebars.

nknapp commented 4 years ago

This issue should be fixed in Handlebars 4.7.2. Could you verify that?

nknapp commented 4 years ago

related to wycats/handlebars.js#1639

MatYRicS commented 4 years ago

It dosen't work for me with v. 4.7.2.

This is my helper file

module.exports = {
    truncate: function (str, num) {
        if (str.length <= num) {
            return str
        }
        return str.slice(0, num) + '...'
    }
};

And adding this to my app.js file:

const truncate = require('./helpers/hbs')

const hbs = exphbs.create({
    helpers: truncate
});

And this is how I use the function in my handlebars file:

            <tbody>
                {{#each systems}}
                <tr>
                    <td class="text-center">{{#each this}} {{ truncate name 10 }} {{/each}} </td>
                    <td class="text-center">{{#each this}} {{table}} {{/each}}</td>
                    <td class="text-center">
                        <button type="button" class="ikon ikon-rundt ikon-rediger" title="Rediger"></button>
                        <button type="button" class="ikon ikon-rundt ikon-slet" title="Slet"></button>
                    </td>
                </tr
...

I keep getting: TypeError: Cannot read property 'length' of undefined