起こった問題


hubotにごはんを選んでもらうをPromiseで書きなおして、Herokuで動かしているhubotの中で使ったら

[TypeError: Object http://ja.wikipedia.org/wiki/Category:料理 has no method ‘then’]


というエラーがでてNodeプロセスが死んで困っていた。ローカルでは動いている。

hubot-gohan/gohan.coffee at be90c1c25219c66295b1c1a10be507c9731cbfaf · shokai/hubot-gohan

  getGohan: ->
debug 'getting Gohan..'
@getPagesCached "#{@baseUrl}/wiki/Category:料理" ## ここでエラー
.then (pages) =>
return new Promise (resolve) =>
categories = _.filter pages, (page) -> /^\/wiki\/Category:/.test page.link
category = _.sample categories
resolve category
.then (category) =>
@getPagesCached "#{@baseUrl}#{category.link}"
.then (pages) =>
return new Promise (resolve) =>
pages = _.filter pages, (page) ->
!(/^\/wiki\/Category:/.test page.link) and
/^\/wiki\/.+/.test(page.link) and
page.title?
debug "got #{pages.length} pages"
gohan = _.sample pages
resolve {url: "#{@baseUrl}#{gohan.link}", title: gohan.title}


解決方法

hubot –createでhubotのテンプレを作ると付属しているcoffee-script npmが1.6.3なので動かない。

新しいcoffee (1.8.x)を入れるとちゃんとPromiseが動くようになる。
% hubot --create my-hubot
% cd my-hubot/
% npm install coffee-script -save