Node.js on Google Cloud Platform Pub/Sub tutorial worker is failing with “TypeError: Cannot call method 'on' of null”

吃可爱长大的小学妹 提交于 2019-12-12 13:28:59

问题


I'm getting an error while working through https://cloud.google.com/nodejs/getting-started/using-pub-sub. (I've successfully completed previous tutorials in the series.)

With the command "SCRIPT=worker.js PORT=8081 npm start", I get this error related to background.js:

TypeError: Cannot call method 'on' of null
    at /Users/xke/Documents/node.js/6-pubsub/lib/background.js:57:20
    at /Users/xke/Documents/node.js/6-pubsub/node_modules/gcloud/lib/pubsub/index.js:256:7
    at /Users/xke/Documents/node.js/6-pubsub/node_modules/gcloud/lib/common/util.js:684:11
    at Object.handleResp (/Users/xke/Documents/node.js/6-pubsub/node_modules/gcloud/lib/common/util.js:192:5)
    at Request._callback (/Users/xke/Documents/node.js/6-pubsub/node_modules/gcloud/lib/common/util.js:679:12)
    at Request.self.callback (/Users/xke/Documents/node.js/6-pubsub/node_modules/request/request.js:354:22)
    at Request.EventEmitter.emit (events.js:98:17)
    at Request.<anonymous> (/Users/xke/Documents/node.js/6-pubsub/node_modules/request/request.js:1207:14)
    at Request.EventEmitter.emit (events.js:117:20)
    at IncomingMessage.<anonymous> (/Users/xke/Documents/node.js/6-pubsub/node_modules/request/request.js:1153:12)

Thoughts / solutions?

Thanks!


回答1:


It appears this is a bug in the gcloud-node library. The library is supposed to automatically create the topic for you, but it appears that there is an edge case that the topic isn't automatically created when you try to subscribe to it.

In the meantime, there is a simple way around this: run the bookshelf app and create / edit a book. It'll cause the library to create the topic. This is because the library does correctly auto-create the topic when you try to publish to it. After that, you should be able to run the worker without error.

Alternatively, you can modify background.js@subscribe to check for the topic's existence and create it if necessary.



来源:https://stackoverflow.com/questions/30677889/node-js-on-google-cloud-platform-pub-sub-tutorial-worker-is-failing-with-typeer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!