我正在尝试将来自节点服务器的数据写入触发GCP云功能的pubsub主题。我的代码或多或少直接从GCP Pubsub tutorial提出来的。
但是,在发布时,我总是收到此错误:
(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)
我已经确认没有消息发送给我的Pubsub主题,并且我的GCP功能没有被触发。
这是我的代码:
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.`);
答案 0 :(得分:1)
对于当前版本的节点客户端库(0.24.1),该教程是正确的。我的猜测是您安装了旧版本的客户端库。检查package.json中的版本,并将其设置为“最新”或“ 0.24.1”。
答案 1 :(得分:0)
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 );