Skip to content

onUpdate (and onCreate) function triggered when document is written for the first time #574

@gagamil

Description

@gagamil

[READ] Step 1: Are you in the right place?

[REQUIRED] Step 2: Describe your environment

  • Operating System version: MacOS Mojave 10.14.4
  • Firebase SDK version:
    Python:
    firebase-admin==3.1.0; google-api-core==1.14.3; google-api-python-client==1.7.11; google-cloud-core==1.0.3; google-cloud-firestore==1.5.0
    Node:
    "firebase-admin": "^8.0.0"; "firebase-functions": "^3.1.0"
  • Library version: _____
  • Firebase Product: Firestore; Functions

[REQUIRED] Step 3: Describe the problem

When using Python Firestore client (Admin SDK) to add (collectionRef.add) a document to a collection both onUpdate and onCreate function triggers are called. Only onCreate should be called.

Note: node Admin SDK produces expected result in functions.

Steps to reproduce:

  • Write and deploy 2 Firestore functions: onCreate and onUpdate
  • Add document with data to a collection
  • Check functions logs and see that the onCreate function was called as well as the onUpdate

Relevant Code:

Functions index.js:

const functions = require("firebase-functions");

exports.onNewMessageLength = functions.firestore
  .document("messages/{messageId}")
  .onCreate((snap, context) => {
    console.log("Data: ", snap.data());
    return 0;
  });

exports.onUpdatedMessageLength = functions.firestore
  .document("messages/{messageId}")
  .onUpdate((change, context) => {
    console.log("Data: ", change.after.data());
    return 0;
  });

Note: #571
Python:

import firebase_admin
from firebase_admin import credentials
firebase_admin.initialize_app()
from google.cloud import firestore
db = firestore.Client()
db.collection('messages').add({'message':'Hello from Python'})

Result: logs show both onCreate and onUpdate are called.

The outcome of the equivalent Node app seems to be correct. Only onCreate called as documented.
Node:

var admin = require("firebase-admin");
var serviceAccount = require(<PATH_TO_FILE_WITH_JSON>)
admin.initializeApp({credential: admin.credential.cert(serviceAccount), databaseURL:<DB_URL>})
const dbfs = admin.firestore();
dbfs.collection('messages').add({message:'Hello from Node'})

Result: only onCreate is called

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions