Google cloud platform 在HTTP触发函数中发布主题
我在GCP中编写了一个HTTP触发函数,它按预期计算一些数据,在计算之后,我想在MQTT主题上发布结果。 我添加了以下代码片段,但它会触发一个错误:Google cloud platform 在HTTP触发函数中发布主题,google-cloud-platform,google-cloud-functions,publish-subscribe,google-cloud-pubsub,Google Cloud Platform,Google Cloud Functions,Publish Subscribe,Google Cloud Pubsub,我在GCP中编写了一个HTTP触发函数,它按预期计算一些数据,在计算之后,我想在MQTT主题上发布结果。 我添加了以下代码片段,但它会触发一个错误: Error: Error: Cannot find module '@google-cloud/pubsub' 下面是添加的代码 //decoding worked const PubSub = require('@google-cloud/pubsub'); // Your Google Cloud Platform project ID c
Error: Error: Cannot find module '@google-cloud/pubsub'
下面是添加的代码
//decoding worked
const PubSub = require('@google-cloud/pubsub');
// Your Google Cloud Platform project ID
const projectId = 'XXXXX';
// Instantiates a client
const pubsubClient = new PubSub({
projectId: projectId
});
// The name for the new topic
const topicName = 'XXXX';
// Creates the new topic
pubsubClient
.createTopic(topicName)
.then(results => {
const topic = results[0];
console.log(`Topic ${topic.name} created.`);
})
.catch(err => {
console.error('ERROR:', err);
});
如果我去掉了库的导入,我就得到了
Error: ReferenceError: PubSub is not defined
因此-如何从gcp中的HTTP触发函数发布主题?您需要将库作为依赖项安装,以便您的云函数能够成功导入它。您可以通过在本地运行以下命令来完成此操作:
npm install --save @google-cloud/pubsub
这将在与函数代码一起上载的package.json文件中包含此库
如果直接从开发者控制台编写函数,则需要将以下内容添加到package.json文件中:
"dependencies": {
"@google-cloud/pubsub": "^0.19.0"
}
好吧,有道理!这些依赖关系对我来说确实不是那么清楚!