1

I'm using heroku to run a node.js app that uses gcloud to create a topic, and then subscribe to it. I'm using the following code, as taken from here: https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.16.0/pubsub

var gcloud = require('gcloud')({
  keyFilename: 'pubsub_key.json',
  projectId: 'pipedrivesekoul'
});

var pubsub = gcloud.pubsub();

//create a new topic
pubsub.createTopic('projects/pipedrivesekoul/my-new-topic', function(err, topic, apiResponse) {
  var topic = pubsub.topic('my-new-topic');
  topic.publish({
    data: 'New message!'
  }, function(err) {console.log});
});

  var topic = pubsub.topic('my-new-topic');



// Without specifying any options.
topic.subscribe('newMessages', function(err, subscription, apiResponse) {});

var alltopics = pubsub.getTopics({}, function(err, topics, nextQuery, apiResponse) {});

console.log(pubsub.getTopics({}, function(err, topics, nextQuery, apiResponse) {}));

However, when I deploy on Heroku (https server, registered on Google Console, with the correct APIs deployed and the appropriate key in a json file), instead of seeing a list of topics, it just returns 'undefined':

2015-07-24T18:06:05.321079+00:00 app[web.1]: undefined

2015-07-24T18:06:05.337947+00:00 app[web.1]: Node app is running on port 36252

Not sure why this might be happening and not too sure how to debug this issue. Any suggestions would be greatly appreciated!

Community
  • 1
  • 1
Sekoul
  • 1,361
  • 2
  • 14
  • 30

1 Answers1

4

I've spotted a couple issues which will hopefully clear this up.

pubsub.createTopic('projects/pipedrivesekoul/my-new-topic'

You only need to provide the my-new-topic part. The ugly, long title is sent automatically.

console.log(pubsub.getTopics({}, function(err, topics, nextQuery, apiResponse) {}));

This is actually logging the result of calling

pubsub.getTopics({}, function(err, topics, nextQuery, apiResponse) {})

Which is undefined. Instead, try:

pubsub.getTopics({}, function(err, topics, nextQuery, apiResponse) {
  if (err) {
    console.error(err);
    return;
  }

  console.log(topics); // hopefully in this array is one called `my-new-topic`
});
Stephen
  • 5,710
  • 1
  • 24
  • 32
  • Great, thank you so much - I can get a list of topics now. I'm trying grant publish rights to a topic using SetIamPolicy as I've described here: [http://stackoverflow.com/questions/31599021/google-cloud-pub-sub-api-push-e-mail/31599464#31599464] but no success yet - not sure what the exact call should look like. For now I've made a json file as described here: [https://developers.google.com/gmail/api/guides/push] and call it like this: `topics.SetIamPolicy('pubsub_policy.json');` - but topics is undefined. Any ideas? Googling has yielded absolutely nothing in terms of examples in node.js... – Sekoul Jul 28 '15 at 17:10