【发布时间】:2019-02-25 04:19:53
【问题描述】:
我被要求创建一个触发 onUpdate 的 firebase (fb) 函数。然后我必须从 fb 数据库中收集一堆信息并发布一条消息,以便在那时触发另一个函数。
fb 更新触发 functionA functionA 发布消息 functionB 是该主题的订阅者,在消息发布后触发。
我了解了以下 onUpdate 触发器的基础知识:
const functions = require("firebase-functions"),
Promise = require("promise"),
PubSub = require(`@google-cloud/pubsub`),
admin = require("firebase-admin");
const pubsub = new PubSub();
exports.checkInOrder = functions.database
.ref("/orders/{id}")
.onUpdate((change, context) => {
const after = change.after.val();
// check the status: "pending-pickup" or "fulfilled" TODO
if (after.status === "new") {
console.log("ended because package is new.");
return null;
}
let dsObj = {};
const orderId = context.params.id;
const topicName = 'check-in-order';
const subscriptionName = 'check-in-order';
return // how would I send the message to the pubsub here?
});
总结一下:
- 如何向 pubsub 发送消息
- 如何订阅 Firebase 函数以在主题收到消息时触发?
如果这听起来很令人困惑,我很抱歉 - 我完全迷路了。谢谢!
【问题讨论】:
-
查看 Pub/Sub trigger docs 和 using Pub/Sub 了解如何将事件发布到主题。
标签: firebase google-cloud-functions google-cloud-pubsub