Ryan
Ryan

Reputation: 750

How do I query a firebase database inside a cloud function?

When a cloud function is triggered based on a specific parent node, how would I be able to query a different parent node within the cloud function, and then update the initial record (that triggered the cloud function) with the queried information? Thanks so much!

Upvotes: 0

Views: 334

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317322

When a database function is triggered, it's delivered a DataSnapshot of the data (or was previously, if it was a change). DataSnapshot has a ref property of type Reference that points to the location of the data. You can use that Reference to build other references by using its parent and root properties, or its child() method:

const root = snapshot.ref.root     // the root of your database
const users = root.child('users')  // the child node under root

You can also use the Admin SDK to build references to other locations, but using your existing ref is more efficient. That ref is already backed by an initialized Admin SDK instance.

To learn how to use a reference, read the documentation on reading and writing data. Prefer to use once() on a reference to read.

Upvotes: 2

Related Questions