希望你们都安全!我需要一些关于Cloud Pub/Sub
触发的云功能的帮助。。。
我有一个由我的一个主题触发的云功能。我想从这条消息向另一个主题发送一条新消息,但我收到了一些错误。
我的功能:
const { PubSub } = require("@google-cloud/pubsub");
exports.helloPubSub = async (message, context) => {
try {
let messageData = Buffer.from(message.data, "base64").toString();
let messageObjectData = JSON.parse(messageData);
//send pub/sub message to another topic
sendPubSubMessage(messageObjectData);
} catch (error) {
console.log(error.message);
}
};
function sendPubSubMessage(
messageObjectData,
topicName = "projects/myproject/topics/mytopic",
data = JSON.stringify(messageObjectData)
) {
const pubSubClient = new PubSub();
async function publishMessage() {
const dataBuffer = Buffer.from(data);
try {
const messageId = await pubSubClient.topic(topicName).publish(dataBuffer);
console.log(`Message ${messageId} published.`);
} catch (error) {
console.error(`Received error while publishing: ${error.message}`);
process.exitCode = 1;
}
}
publishMessage();
}
Package.json:
{
"name": "sample-pubsub",
"version": "0.0.1",
"dependencies": {
"@google-cloud/pubsub": "^0.18.0"
}
}
当这个函数运行时,我会得到以下错误:ERROR: PubSub is not a constructor
我试图将依赖关系版本更改为上一个2.18.3
,但后来它没有部署。。。
我还尝试加载两个版本:
{
"name": "sample-pubsub",
"version": "0.0.1",
"dependencies": {
"@google-cloud/pubsub": "^0.18.0",
"lastPubSub" : "npm:google-cloud/pubsub@^2.18.3",
}
}
得到了同样的错误。。。
有人知道怎么修吗?
提前感谢!
谢谢大家的评论,我终于收到了…
解决方案:
package.json
:
{
"name": "sample-pubsub",
"version": "0.0.1",
"dependencies": {
"@google-cloud/pubsub": "^0.18.0",
"googlePub": "npm:@google-cloud/pubsub@^2.18.3"
}
}