简体   繁体   中英

NodeJS GCP pubsub publishing error: t.topic(…).publish is not a function

I am trying to write data from my Node server to a pubsub topic that triggers a GCP Cloud Function. My code is more or less lifted straight from the GCP Pubsub tutorial .

However, I keep getting this error when I publish:

(node:60085) UnhandledPromiseRejectionWarning: TypeError: l.topic(...).publish is not a function
at /Users/kb/Documents/coding/maple/dist/server.bundle.js:100:1697
at Layer.handle [as handle_request] (/Users/kb/Documents/coding/maple/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/kb/Documents/coding/maple/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/kb/Documents/coding/maple/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/kb/Documents/coding/maple/node_modules/express/lib/router/layer.js:95:5)
at /Users/kb/Documents/coding/maple/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/kb/Documents/coding/maple/node_modules/express/lib/router/index.js:335:12)
at next (/Users/kb/Documents/coding/maple/node_modules/express/lib/router/index.js:275:10)
at SendStream.error (/Users/kb/Documents/coding/maple/node_modules/serve-static/index.js:121:7)
at SendStream.emit (events.js:182:13)

I've confirmed that no messages are getting sent to my Pubsub topic, and that my GCP function isn't getting triggered.

Here is my code:

var processedData = processResultsData(data);
const dataBuffer = Buffer.from(processedData);
console.log("About to push to pubsub");
const messageId = await pubsub.topic(TOPIC_NAME).publish(dataBuffer);
console.log(`Message ${messageId} published.`);

The tutorial is correct for the current version of the node client libraries (0.24.1). My guess is that you have an old version of the client libraries installed. Check which version is in your package.json, and set it to "latest" or "0.24.1".

let message = {
    id : `your-unique-id`,
    data : [ {"key", "value"} , {"otherKey", "otherValue"} ]
  }

  const dataBuffer = Buffer.from(JSON.stringify(message));
  const messageId = await pubsub.topic(topicName).publish(dataBuffer);
  console.log('published : ', messageId );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM