最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Firebase v2 Database Triggers not able access parent data from within trigger - Stack Overflow

programmeradmin0浏览0评论

It seems as if my promise to get parent data from within my Realtime Database trigger doesn't resolve.

Realtime Database Structure:

{
  matches: {
    {matchId}: {
      startAt: number,
      endAt: number,
    }
  }
}

I am manually creating a new child in matches/{matchId} called endTimeExtensionVote and setting it to true using the Firebase Console.

I'm using a Realtime Database Trigger to capture this onValueCreated event and I simply want to log the details of the parent (the full data from the {matchId} object) in the cloud run console. Currently this is not working no matter what way I return the promise.

My Cloud Function:

import {onValueCreated} from "firebase-functions/v2/database";
import * as admin from "firebase-admin";

if (admin.apps.length === 0) {
  admin.initializeApp();
}


export const forceTimeExtensionVote = onValueCreated({ref: "/matches/{matchId}/timeExtensionVoteEnded", region: "europe-west1"}, (event) => {
  console.log("Force Time Extenion Vote trigger running...");
  return event.data.ref.parent?.ref.once("value").then((snapshot) => {
    console.log("Log parent data", snapshot.val());
  }).catch((error) => {
    console.log(error);
    throw error;
  });
});

Currently I'm not doing anything with the data, just trying to log the snapshot to the console.

Expected Result: First log indicating the trigger is running, second log logging the snapshot value.

Actual Result: Only the first log indicating the trigger is running comes through.

DEMAND 1: Log event.data and event.data.ref.parent to assert they are not null

  • Code
export const forceTimeExtensionVote = onValueCreated({ref: "/matches/{matchId}/timeExtensionVoteEnded", region: "europe-west1"}, (event) => {
  console.log("Force Time Extenion Vote trigger running...");
  console.log("Event data", event.data.toString());
  console.log("Event data parent", event.data.ref.parent.toString());
  return event.data.ref.parent?.ref.once("value").then((snapshot) => {
    console.log("Log parent data", snapshot.val());
  }).catch((error) => {
    console.log(error);
    throw error;
  });
});
  • Output
2025-03-04 23:29:35.099 GMT
Force Time Extenion Vote trigger running...
2025-03-04 23:29:35.102 GMT
Event data DataSnapshot { app: FirebaseApp { appStore: AppStore { appStore: [Map] }, services_: { storage: [Storage] }, isDeleted_: false, name_: '[DEFAULT]', options_: { projectId: 'my-db', databaseURL: '', storageBucket: 'my-db.appspot', locationId: 'europe-west', credential: [ComputeEngineCredential] }, INTERNAL: FirebaseAppInternals { credential_: [ComputeEngineCredential], tokenListeners_: [], isRefreshing: false }, auth: [Function (anonymous)], appCheck: [Function (anonymous)], database: [Function (anonymous)], messaging: [Function (anonymous)], storage: [Function (anonymous)], firestore: [Function (anonymous)], instanceId: [Function (anonymous)], installations: [Function (anonymous)], machineLearning: [Function (anonymous)], projectManagement: [Function (anonymous)], securityRules: [Function (anonymous)], remoteConfig: [Function (anonymous)], __extended: true }, instance: '', _path: 'matches/9e29affd-7dcd-48b8-90ca-cca7fc7849ce/timeExtensionVoteEnded', _data: true }
2025-03-04 23:29:35.109 GMT
Event data parent https://my-db/matches/9e29affd-7dcd-48b8-90ca-cca7fc7849ce

They are indeed not null but the issue with the promise persists

发布评论

评论列表(0)

  1. 暂无评论