Jama Mohamed
Jama Mohamed

Reputation: 3405

How to access uid using Cloud Functions for Firebase?

I wanted to access the uid of a user using javascript on the node environment and creating another reference to the accessed value "name" . My code and firebase data structure is as follows

exports.getData = functions.database.ref('/Followers/{uid}/{uid}/name')
    .onWrite(event => {

      //Getting the name data value

      const name = event.data.val();

      return admin.database().ref('/Accessed Name').set(name);
    });

My Firebase sample data structure:

1

The accessed data "name" should create a reference such as the example below.

The accessed data "name"

Upvotes: 1

Views: 2751

Answers (2)

Peter Haddad
Peter Haddad

Reputation: 80914

Cloud functions V1.0

To be able to get the uid in the version 1.0, you need to do the following:

exports.getData = functions.database.ref('/Followers/{uid1}/{uid2}/name')
.onWrite((change, context) => {

 const uid1 = context.params.uid1
 const uid2 = context.params.uid2

});

The data parameter represents the data that triggered the function.

The context parameter provides information about the function's execution.

more info here:

https://firebase.google.com/docs/functions/beta-v1-diff

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317372

To get the wildcard values from the matched path, you can use the event parameters:

exports.getData = functions.database.ref('/Followers/{uid1}/{uid2}/name')
    .onWrite(event => {

    const uid1 = event.params.uid1
    const uid2 = event.params.uid2

}

Note that you have to give them different names in the path to get different values. In your example, you gave them the same name.

See the documentation for database triggers for more information.

Upvotes: 15

Related Questions