Google Cloud Pub/Sub Node.js Sample: TypeError: Cannot read property 'on' of null -
i'm using gcp , want use cloud pub/sub. got error below when tried node.js sample. knows how fix it?
/private/tmp/pubsub/pubsubsample.js:26 subscription.on('error', onerror); ^ typeerror: cannot read property 'on' of null @ /private/tmp/pubsub/pubsubsample.js:26:15 @ /private/tmp/pubsub/node_modules/gcloud/lib/pubsub/index.js:474:7 @ object.handleresp (/private/tmp/pubsub/node_modules/gcloud/lib/common/util.js:113:3) @ /private/tmp/pubsub/node_modules/gcloud/lib/common/util.js:422:12 @ request.onresponse [as _callback] (/private/tmp/pubsub/node_modules/gcloud/node_modules/retry-request/index.js:106:7) @ request.self.callback (/private/tmp/pubsub/node_modules/gcloud/node_modules/request/request.js:198:22) @ emittwo (events.js:87:13) @ request.emit (events.js:172:7) @ request.<anonymous> (/private/tmp/pubsub/node_modules/gcloud/node_modules/request/request.js:1035:10) @ emitone (events.js:82:20)
https://github.com/googlecloudplatform/gcloud-node
var gcloud = require('gcloud'); // authenticating on per-api-basis. don't need if // auth on global basis (see authentication section above). var pubsub = gcloud.pubsub({ projectid: 'xxxxx', keyfilename: 'xxx.json' }); // reference topic has been created. var topic = pubsub.topic('info'); // publish message topic. topic.publish({ data: 'new message!' }, function(err) {}); // subscribe topic. topic.subscribe('new-subscription', function(err, subscription) { // register listeners start pulling messages. function onerror(err) {} function onmessage(message) {} subscription.on('error', onerror); subscription.on('message', onmessage); // remove listeners stop pulling messages. subscription.removelistener('message', onmessage); subscription.removelistener('error', onerror); });
... i'm using pubsub i'm thinking whether can same thing using google cloud pubsub.
this post may relevant. node.js on google cloud platform pub/sub tutorial worker failing "typeerror: cannot call method 'on' of null"
update 1
i changed code same error showed.
(error)
subscription.on('error', onerror); ^ typeerror: cannot read property 'on' of null
(code)
// subscribe topic topic.subscribe('new-subscription', function(err, subscription) { if( err ) { // went wrong, react! return; } // register listeners start pulling messages. function onerror(err) {} function onmessage(message) {} subscription.on('error', onerror); subscription.on('message', onmessage); // remove listeners stop pulling messages. subscription.removelistener('message', onmessage); subscription.removelistener('error', onerror); });
update 2
my expectation below.
- execute "node pubsub.js"
- i can see sample message 'new message!'
when invoking topic.subscribe()
, method invokes pubsub.subscribe()
method specific topic instance context. can seen source code under topic.prototype.subscribe
.
based on pubsub index.js source code, pubsub.prototype.subscribe()
issues http request create new subscription respecting projects.subscriptions.create
api format. if response returns 409 already_exists
error , have not set options.reuseexisting
true
, callback provided invoked error , null
subscription. according pubsub nodejs documentation topic.subscribe
under options.reuseexisting
, default value if not specified false
,
if false, attempting create subscription exists fail.
to use design more effectively, suggest following:
var pubsub = require('gcloud').pubsub({ "projectid" : "project-id", "keyfilename" : "key-file.json" }); // topic should have been created var topic = pubsub.topic("interesting-topic"); // message published var message = {"data": "welcome interesting thread"}; // callback throw exception if publish unsuccessful // log published message ids if successful function publishedhandler(err, messageids, responsebody) { if (err) { // not publish message(s) throw err; } console.log(messagesids); } // callback throw exception if subscription not found or created // attach event listeners if gets subscription function subscriptionhandler(err, subscription, responsebody) { if (err) { // not or create new subscription throw err; } subscription.on("error", errorhandler); subscription.on("message", messagehandler); } // publish message topic topic.publish(message, publishedhandler); // create or 'sub' , subcribe 'interesting-topic' topic.subscribe("sub", {"reuseexisting": true}, subscriptionhandler);
issue 696 may not refer issue discuss of semantic choices made in designing node.js library cloud pubsub. it's not entirely clear if methods topic.publish
or topic.subscribe
check existence of topic, existence of subscription, create new subscription or existing subscription. warn add robust error handling.
Comments
Post a Comment