0

I'm trying to add documents to firestore using add() method. As per the documentation add() method generates a unique ID for each document and stores it in any random order and to store it in an ordered manner we need to a timestamp field in document. Here's how I'm doing it:

def add_data(message):
  message.update({u'timestamp': firestore.SERVER_TIMESTAMP})
  coll_ref.add(message)

When I'm executing the above method I'm getting an error:

TypeError: ('Cannot convert to a Firestore Value', <object object at 0x104fdebe0>, 'Invalid type', <class 'object'>)

Why am I getting this error? Is there a bug in firestore Python SDK, if so then any patch has been released for this issue?

1 Answer 1

1

Following worked for me with the latest version of the Python Admin SDK (verified in the REPL).

>>> import firebase_admin
>>> from firebase_admin import firestore
>>> firebase_admin.initialize_app()
<firebase_admin.App object at 0x1080c33d0>
>>> client = firestore.client()
>>> msg = {'timestamp': firestore.SERVER_TIMESTAMP}
>>> foo = client.collection('foo')
>>> foo.add(msg)
(seconds: 1569404958
nanos: 428866000
, <google.cloud.firestore_v1.document.DocumentReference object at 0x108dfc610>)

This uses v1.4.0 of google-cloud-firestore underneath. Check your library versions, imports etc.

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

3 Comments

I'm using v0.31.0 of google-cloud-firestore. I was right then it is because of improper version. Is there any work around for this if I plan on sticking with v0.31.0?
Unlikely. What is preventing you from upgrading?
Managerial constraints.

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.