Как я могу вытащить паб / суб-сообщения из облачной функции? - PullRequest
0 голосов
/ 19 марта 2020

Я хочу использовать облачную функцию для извлечения сообщений из pub / sub. Поэтому я установил пулл-подписку на топи c. Затем я запускаю функцию Cloud с Cloud Scheduler каждый час.

  1. Есть ли лучший подход, если мне нужно использовать pull?

Я скопировал пример из документации , но выдает ошибку.

 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);

};

Спасибо!

1 Ответ

1 голос
/ 19 марта 2020

В этой строке выдается сообщение об ошибке:

const formattedSubscription = subClient.subscriptionPath(projectId, subscriptionName);

, потому что ваша 'name_of_my_subscription' равна 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...