2
import * as functions from 'firebase-functions';

console.log('I am a log entry0!');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
console.log('I am a log entry1!');

export const onMessageCreate = functions.database
.ref('/users/{userId}/totalScore')
.onUpdate((change) => {
    console.log('I am a log entry2!');
    //var a = admin.firestore().collection('/users');
})

I have deployed the function and I can see it in the console. But the function is not executed when totalScore is updated in the database....

enter image description here

2
  • Do you mean that you don't see the log for 'I am a log entry2!'? Commented Feb 1, 2019 at 8:11
  • I don't see any log entry (except for the first time when I deploy the function). Neither do I see any calls in the firebase console Commented Feb 1, 2019 at 8:39

3 Answers 3

4

Your database is Firestore but you use a Cloud Function that is triggered by an update in the Realtime Database. These are two different Firebase services and you need to change your code accordingly.

The following code will work:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.onMessageCreate = functions.firestore
  .document('users/{userId}')
  .onUpdate((change, context) => {
    // Get an object representing the document
    const newValue = change.after.data();

    // ...or the previous value before this update
    const previousValue = change.before.data();

    if (newValue.totalScore !== previousValue.totalScore) {
      console.log('NEW TOTAL SCORE');
    }

    return null;

    //I guess you are going to do more than just logging to the console.
    //If you do any asynchronous action, you should return the corresponding promise, see point 3 below

    //For example:
    //if (newValue.totalScore !== previousValue.totalScore) {
    //    return db.collection("score").doc(newValue.name).set({score: newValue.totalScore});
    //}

  });

Note that:

  1. You cannot trigger the onUpdate Cloud Function when a specific field of the document changes. The Cloud Function will be triggered when any field of the Firestore document changes. But you can detect which field(s) have changed, as shown in the above code.
  2. Since version 1.0 you have to initialize with admin.initializeApp();, see https://firebase.google.com/docs/functions/beta-v1-diffList
  3. You need to indicate to the platform when the Cloud Function has finished executing: Since you are not executing any asynchronous operation in your Cloud Function you can use return null;. (For more details on this point, I would suggest you watch the 3 videos about "JavaScript Promises" from the Firebase video series: https://firebase.google.com/docs/functions/video-series/).
Sign up to request clarification or add additional context in comments.

7 Comments

how do you try to fire it? Do you update an existing totalScore node?
i have uploaded a picture of the node
Ok, now I understand! Your database is Firestore but you use a Cloud Function that is triggered by an update in the Realtime Database. These are two different Firebase services. I'll update my answer.
i always get error: [ts] Parameter 'change' implicitly has an 'any' type. [7006] and [ts] 'context' is declared but its value is never read. [6133] [ts] Parameter 'context' implicitly has an 'any' type. [7006]
|
1

I think the update is checked on the ref not on the child Try this

export const onMessageCreate = functions.database
.ref('/users/{userId}')
.onUpdate((change) => {
    console.log('I am a log entry2!');
    //var a = admin.firestore().collection('/users');
})

You get the old and new values of the snapshot at that location

1 Comment

still no executionm although totalScore has changed :/
1

If you are using Cloud Firestore then your listener is incorrect. In your case, you are specifying a listener for Realtime Database. We extract firestore from the functions and specify the path to the document we want to have a listener on:

import * as functions from 'firebase-functions';

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

export const onMessageCreate = functions.firestore
.document('users/{userId}')
.onUpdate((change, context) => {
    console.log(change.before.data()); // shows the document before update
    console.log(change.after.data()); // shows the document after change
    return; 
})

3 Comments

This Cloud Function will probably not deploy correctly due to the path users/{userId}/{documentId}.
Hey, you are right. :) The {documentId} should not be there. I will edit it. I saw your answer which should fix the issue.
i always get error: [ts] Parameter 'change' implicitly has an 'any' type. [7006] and [ts] 'context' is declared but its value is never read. [6133] [ts] Parameter 'context' implicitly has an 'any' type. [7006]

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.