我想使用Cloud Function从发布/订阅中提取消息。因此,我为主题设置了强制订阅。然后,我每小时都会使用Cloud Scheduler触发Cloud Function。
我已经从文档中复制了一个示例,但它引发了错误。
TypeError: match error: could not instantiate a path template from
projects/projectname/subscriptions/projects/projectname/subscriptions/test at PathTemplate.match
(/srv/node_modules/google-gax/build/src/pathTemplate.js:82:19) at PathTemplate.render
(/srv/node_modules/google-gax/build/src/pathTemplate.js:118:14) at SubscriberClient.subscriptionPath
(/srv/node_modules/@google-cloud/pubsub/build/src/v1/subscriber_client.js:1838:57) at
exports.helloPubSub (/srv/index.js:22:45) at /worker/worker.js:825:24 at <anonymous> at
process._tickDomainCallback (internal/process/next_tick.js:229:7)
我复制的代码如下:
const pubsub = require('@google-cloud/pubsub');
const subClient = new pubsub.v1.SubscriberClient();
const subscriptionName = 'name_of_my_subscription';
const projectId = 'my_project_id';
const timeout = 60;
exports.helloPubSub = async (event, context) => {
const formattedSubscription = subClient.subscriptionPath(projectId, subscriptionName);
const request = {
subscription: formattedSubscription,
maxMessages: 10,
};
const [response] = await subClient.pull(request);
const ackIds = [];
for (const message of response.receivedMessages) {
console.log(`Received message: ${message.message.data}`);
ackIds.push(message.ackId);
}
const ackRequest = {
subscription: formattedSubscription,
ackIds: ackIds,
};
await subClient.acknowledge(ackRequest);
};
谢谢!
答案 0 :(得分:1)
此行出现错误信息:
const formattedSubscription = subClient.subscriptionPath(projectId, subscriptionName);
因为您的“我的姓名预订”为projects/your-project/subscriptions/subname
,而不是subname
:
const subscriptionName = 'name_of_my_subscription';
这就是为什么您会获得双名projects/projectname/subscriptions/
的原因:
could not instantiate a path template from
projects/projectname/subscriptions/projects/projectname/subscriptions/test