DRYな備忘録

Don't Repeat Yourself.

undefined is not a function【Handlebars】

問題

  • handlebars使ってて掲題のエラー出た

f:id:otiai10:20141103181405j:plain

"undefined is not a function" とは

  • ご存知、未定義の変数を関数呼び出しした時に起きるエラー
var my = {
    foo: function() {
        console.log("This is my.foo!");
    }
    /*
    bar: function() {
        console.log("This is my.bar, undefined!!");
    }
    */
};

var foo = my["foo"];
foo();
// This is my.foo!

var bar = my["bar"];
bar();
// TypeError: undefined is not a function

調査

  • 上記ではapp.jsの2212行目でエラーが出ているので見に行く
2210     return function(context, options) {
2211       options = options || {};
2212       var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
2213

debuggerなど仕込むと、どうやらこのtemplateSpecFunctionオブジェクトではないので、callメソッドが無いっぽい

f:id:otiai10:20141103182527j:plain

さらに調査

「Handlebarsのバージョンが古いんじゃねえの?」とのご指摘

解決

--- a/bower.json
+++ b/bower.json
@@ -4,6 +4,6 @@
   "dependencies": {
     "showv": "https://github.com/otiai10/showv.git",
     "jquery": "1.10.1",
-    "handlebars": "1.0.0"
+    "handlebars": "2.0.0"
   }
 }

DRYな備忘録として