admin管理员组文章数量:1410730
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
本文标签: nodejsFirebase v2 Database Triggers not able access parent data from within triggerStack Overflow
版权声明:本文标题:node.js - Firebase v2 Database Triggers not able access parent data from within trigger - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745033795a2638662.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论