【发布时间】:2020-05-02 15:22:37
【问题描述】:
我正在尝试在 Firestore 中写入文档时向 GCP PubSub 发布消息。
我已经让它工作了,但它的功能被列为已弃用。当我尝试使用较新的功能时出现错误。
我正在使用来自here 的文档。 publish 被列为已弃用,并指向 publishMessage 作为其替代品。
我在使用 publishMessage 函数时收到的错误是“TypeError: Data must be in a Buffer.”
知道我在 publishMessage 语法中缺少什么吗?
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const firestore = admin.firestore();
const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub(MY_PROJECT);
exports.pubSubPublishTest = functions.firestore.document('pubSubTest/{docID}').onWrite((change, context) => {
const topic = pubsub.topic('test');
const otherBuffer = Buffer.from('this is the message');
const callback = (err, messageId) => {
if (err) {
console.error(`error encountered during publish - ${err}`);
} else {
console.log(`Message ${messageId} published.`);
}
};
// this worked, but the function is listed as deprecated
topic.publish(otherBuffer, callback);
// this did not work - {otherBuffer} is from the doc
// but I also tried without the curly braces and received the same error.
//topic.publishMessage({otherBuffer}, callback);
return null;
});
【问题讨论】:
标签: javascript node.js google-cloud-firestore google-cloud-functions google-cloud-pubsub