Skip to content

ValueError from firestore_fn.on_document_created (Emulator) #150

Closed as not planned
@gavisio

Description

@gavisio

There's a bug when the @firestore_fn.on_document_created() gets executed in the emulator.
I started with this example.

# The Cloud Functions for Firebase SDK to create Cloud Functions and set up triggers.
from firebase_functions import firestore_fn, https_fn

# The Firebase Admin SDK to access Cloud Firestore.
from firebase_admin import initialize_app, firestore
import google.cloud.firestore

app = initialize_app()


@https_fn.on_request()
def addmessage(req: https_fn.Request) -> https_fn.Response:
    """Take the text parameter passed to this HTTP endpoint and insert it into
    a new document in the messages collection."""
    # Grab the text parameter.
    original = req.args.get("text")
    if original is None:
        return https_fn.Response("No text parameter provided", status=400)

    firestore_client: google.cloud.firestore.Client = firestore.client()

    # Push the new message into Cloud Firestore using the Firebase Admin SDK.
    _, doc_ref = firestore_client.collection("messages").add(
        {"original": original}
    )

    # Send back a message that we've successfully written the message
    return https_fn.Response(f"Message with ID {doc_ref.id} added.")


@firestore_fn.on_document_created(document="messages/{pushId}")
def makeuppercase(
    event: firestore_fn.Event[firestore_fn.DocumentSnapshot | None],
) -> None:
    """Listens for new documents to be added to /messages. If the document has
    an "original" field, creates an "uppercase" field containg the contents of
    "original" in upper case."""

    # Get the value of "original" if it exists.
    if event.data is None:
        return
    try:
        original = event.data.get("original")
    except KeyError:
        # No "original" field, so do nothing.
        return

    # Set the "uppercase" field.
    print(f"Uppercasing {event.params['pushId']}: {original}")
    upper = original.upper()
    event.data.reference.update({"uppercase": upper})

When I now open the url http://localhost:5001/MY_PROJECT/us-central1/addMessage?text=uppercaseme

I get the following error in the console:

i  functions: Beginning execution of "us-central1-addmessage"
i  functions: Finished "us-central1-addmessage" in 48.166834ms
i  functions: Beginning execution of "us-central1-makeuppercase"
>  [2023-10-14 14:04:37,923] ERROR in app: Exception on /functions/projects/ [POST]
>  Traceback (most recent call last):
>    File "/Users/XXX/Firebase/functions/venv/lib/python3.11/site-packages/flask/app.py", line 2190, in wsgi_app
>      response = self.full_dispatch_request()
>                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/XXX/Firebase/functions/venv/lib/python3.11/site-packages/flask/app.py", line 1486, in full_dispatch_request
>      rv = self.handle_user_exception(e)
>           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/XXX/Firebase/functions/venv/lib/python3.11/site-packages/flask/app.py", line 1484, in full_dispatch_request
>      rv = self.dispatch_request()
>           ^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/XXX/Firebase/functions/venv/lib/python3.11/site-packages/flask/app.py", line 1469, in dispatch_request
>      return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
>             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/XXX/Firebase/functions/venv/lib/python3.11/site-packages/functions_framework/__init__.py", line 174, in view_func
>      function(event)
>    File "/Users/XXX/Firebase/functions/venv/lib/python3.11/site-packages/firebase_functions/firestore_fn.py", line 308, in on_document_created_wrapped
>      return _firestore_endpoint_handler(
>             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/XXX/Firebase/functions/venv/lib/python3.11/site-packages/firebase_functions/firestore_fn.py", line 115, in _firestore_endpoint_handler
>      is_nanoseconds = _util.is_precision_timestamp(time)
>                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/XXX/Firebase/functions/venv/lib/python3.11/site-packages/firebase_functions/private/util.py", line 332, in is_precision_timestamp
>      _, s_fraction = time.split(".")
>      ^^^^^^^^^^^^^
>  ValueError: not enough values to unpack (expected 2, got 1)
i  functions: Finished "us-central1-makeuppercase" in 5.738042ms

Didn't changed anything in the requirements.txt file:
firebase_functions~=0.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions