【发布时间】:2016-01-27 07:38:43
【问题描述】:
我正在使用 GCP,我想使用 Cloud Pub/Sub。当我尝试 Node.js 示例时,我在下面收到此错误。有人知道怎么解决吗?
/private/tmp/pubsub/pubsubsample.js:26
subscription.on('error', onError);
^
TypeError: Cannot read property 'on' of null
at /private/tmp/pubsub/pubsubsample.js:26:15
at /private/tmp/pubsub/node_modules/gcloud/lib/pubsub/index.js:474:7
at Object.handleResp (/private/tmp/pubsub/node_modules/gcloud/lib/common/util.js:113:3)
at /private/tmp/pubsub/node_modules/gcloud/lib/common/util.js:422:12
at Request.onResponse [as _callback] (/private/tmp/pubsub/node_modules/gcloud/node_modules/retry-request/index.js:106:7)
at Request.self.callback (/private/tmp/pubsub/node_modules/gcloud/node_modules/request/request.js:198:22)
at emitTwo (events.js:87:13)
at Request.emit (events.js:172:7)
at Request.<anonymous> (/private/tmp/pubsub/node_modules/gcloud/node_modules/request/request.js:1035:10)
at emitOne (events.js:82:20)
https://github.com/GoogleCloudPlatform/gcloud-node
var gcloud = require('gcloud');
// Authenticating on a per-API-basis. You don't need to do this if you
// auth on a global basis (see Authentication section above).
var pubsub = gcloud.pubsub({
projectId: 'xxxxx',
keyFilename: 'xxx.json'
});
// Reference a topic that has been previously created.
var topic = pubsub.topic('info');
// Publish a message to the topic.
topic.publish({
data: 'New message!'
}, function(err) {});
// Subscribe to the topic.
topic.subscribe('new-subscription', function(err, subscription) {
// Register listeners to start pulling for messages.
function onError(err) {}
function onMessage(message) {}
subscription.on('error', onError);
subscription.on('message', onMessage);
// Remove listeners to stop pulling for messages.
subscription.removeListener('message', onMessage);
subscription.removeListener('error', onError);
});
...我现在正在使用 PubSub,但我正在考虑是否可以通过使用 Google Cloud PubSub 来做同样的事情。
更新 1
我更改了此代码,但显示了相同的错误。
(错误)
subscription.on('error', onError);
^
TypeError: Cannot read property 'on' of null
(代码)
// Subscribe to the topic
topic.subscribe('new-subscription', function(err, subscription) {
if( err ) {
// something went wrong, react!
return;
}
// Register listeners to start pulling for messages.
function onError(err) {}
function onMessage(message) {}
subscription.on('error', onError);
subscription.on('message', onMessage);
// Remove listeners to stop pulling for messages.
subscription.removeListener('message', onMessage);
subscription.removeListener('error', onError);
});
更新 2
我的期望如下。
- 执行“节点 pubsub.js”
- 我可以看到示例消息“新消息!”
【问题讨论】:
-
在使用
subscription之前,subscribe()回调当前不检查err参数。我敢打赌,err告诉你,出了什么问题。 -
@Sirko 感谢您的回复。我仍然不确定下一步该怎么做...您能给我更多建议吗..
-
很抱歉我删除了标签。
-
非常感谢。只是想确保您在任何事情上都不需要我们的帮助。干杯。
标签: google-cloud-messaging google-compute-engine publish-subscribe google-cloud-platform