我正在尝试使用 Firebase 日志。我能够将新记录添加到我的 Firebase 实时数据库Chat
实体中。
问题是,我在 Google Cloud Log Explorer 中浏览它们时看不到任何日志或错误。
我使用以下命令从我的文件夹成功提交:
gcloud builds submit --tag gcr.io/relay-world/realtime-database-handler .
我按照建议迁移到工件注册表,并创建了以下文件夹:
asia.gcr.io
cloud-run-source-deploy
eu.gcr.io
gcf-artifacts
gcr.io
us.gcr.io
如果我打开gcr.io
文件夹,就会看到 realtime-database-handler
里面的内容。我的目标是向用户的设备发送推送通知,但目前我只想记录数据。代码如下:
const admin = require("firebase-admin");
const { PubSub } = require("@google-cloud/pubsub");
console.log("BEFORE APP INIT");
// Initialize Firebase Admin SDK
admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: "https://relay-world.firebaseio.com", // Replace with your database URL
});
// Initialize Pub/Sub
const pubsub = new PubSub();
const topicName = "realtime-database-chat-topic";
// Reference the Realtime Database path
const db = admin.database();
const ref = db.ref("Chat");
console.log("Listening for Realtime Database changes...");
// Listen for new children added to the collection
ref.on("child_added", async (snapshot) => {
const newValue = snapshot.val();
const childId = snapshot.key;
console.log(`New child added: ${childId}`, newValue);
// Publish a message to the Pub/Sub topic
const message = {
data: {
id: childId,
...newValue,
},
};
try {
const messageId = await pubsub.topic(topicName).publishMessage({
json: message,
});
console.log(`Message published with ID: ${messageId}`);
} catch (error) {
console.error("Error publishing message:", error);
}
});
你知道问题可能出在哪里吗?非常感谢!