1

In my firebase database, I have a "users" node.

I use Firebase Functions for backend functions. My javascript code (node.js) running in Functions needs to do something when a new user is added to database, here is the backend function code:

    // required modules
    const functions = require('firebase-functions');
    const admin = require('firebase-admin');

    // my backend function
    exports.onUserCreated = functions.auth.user().onCreate(event => {
      // Do something when a new user is created
    }

I successfully deployed my backend function to firebase Functions.

Then, I manually added a user under users node of firebase database. Then, I go to Firebase Functions, I see the onUserCreated Function listed there in console, but the number of executions is 0.

What could be the reason why my function is not triggered when I manually added a user in database?

2 Answers 2

3

Your function is an authentication trigger, not a database trigger:

functions.auth.user().onCreate(...)

It will trigger when a new user is added via Firebase Authentication, not when there is a change in your database.

If you want to write a database trigger, follow the instructions here instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Is there a way to quickly test the authentication triggered event without run real application->login ?
1

What's happening is that you've created a function that is triggered by Firebase Auth events. Which means that it will only be triggered when a new User signs up to use your application. Not when he writes to the database.

If you want it to be triggered when you create a new user under the "users" node, you should use a Realtime Database trigger:

exports.onUserCreated = functions.database.ref('/user/{userId}')
    .onWrite(event => {
      // Do something when a new user is created
    }

EDIT: Accessing the user's email stored in the database:

exports.onUserCreated = functions.database.ref('/user/{userId}')
    .onWrite(event => {
      // Do something when a new user is created
      var email = event.data.val().email;
    }

6 Comments

Thanks! Is there a way to quickly test the authentication triggered event without run real application->login ?
You can use the cloud functions local emulator, by passing a UserRecord
Another question: in your code sample, how can I get the added user's login email address in the function body?
You cant' unless you're saving this email address to the database. If you are, check my updated answer.
Hmmm....I tried login with facebook (well, the app doesn't ask me to input username password since I think the Facebook/Gmail authentication automatically use the token generated from 1st time login), but the backend function is not triggered either. Does it have to be 1st time login to trigger my backend function?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.