0

I can't find the answer to this in the documentation. I have created a firebase cloud function which listens to the database and gets triggered when a doc in the database changes.

What I want the function to do is to gather information from the database, other than the information listened to in the first place, and then work with that information. How would I gather information from the Firestore database using firebase admin?

Down below is the code I have written so far:

exports.costCalculation = functions.firestore
  .document("questions/{docId}/post_publish_information/outcome_information")
  .onUpdate((change, context) => {
    const predictions = admin
      .firestore()
      .document("questions/{docId}/post_publish_information/predictions");
    console.log(predictions);
  });

GabrielJSW
  • 184
  • 2
  • 10

1 Answers1

0

The Admin SDK is already loaded and initialized, you just can't access it through the admin variable. If you don't feel like initializing an Admin SDK instance yourself, you can work from the document you get it from change.

For example, to get a reference to the predictions document you can also:

change.before.ref.parent.doc("predictions")
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • That gave me the following error: TypeError: change.before.ref.parent.document is not a function – GabrielJSW Apr 26 '22 at 23:17
  • Ah yeah, I see it's called `doc`. Updated the code in my answer, but I also recommend you keep the reference documentation handy to most quickly fix such typos: https://googleapis.dev/nodejs/firestore/latest/CollectionReference.html – Frank van Puffelen Apr 27 '22 at 01:39