Skip to content

ValueError from firestore_fn.on_document_created (Emulator) #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gavisio opened this issue Oct 14, 2023 · 5 comments
Closed

ValueError from firestore_fn.on_document_created (Emulator) #150

gavisio opened this issue Oct 14, 2023 · 5 comments

Comments

@gavisio
Copy link

gavisio commented Oct 14, 2023

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

@erdenebayrd
Copy link

Same

@antont
Copy link

antont commented Oct 22, 2023

Is fixed there, #149

They are making a 0.2.0 release very soon now, the version num was already changed in main and all the code is in.

@EmilioNicolas
Copy link

Looking forward to the new release 👍 , any scheduled date?

@antont
Copy link

antont commented Oct 24, 2023

Any day now I guess, perhaps Google has some process it's going through still, dunno.

BTW I got the version number wrong, it's 0.1.2 in main now, 8926c71

@exaby73
Copy link
Contributor

exaby73 commented Oct 24, 2023

Hey. Since this issue is fixed in #149, I'll close this issue. 0.1.2 will be released soon with this fix. Thanks for your patience

@exaby73 exaby73 closed this as not planned Won't fix, can't repro, duplicate, stale Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants