OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

AttributeError: _nanosecond when updating a datetime in transaction

  • Thread starter Thread starter Adarsh
  • Start date Start date
A

Adarsh

Guest
So I am trying to update a datetime field in cloud firestore via cloud function as follows:

Code:
transaction.update(doc_ref, {'dateTimeField1': dateTimeValue})

Google sends datetime objects as string in event param of the cloud function in the format %Y-%m-%dT%H:%M:%SZ or %Y-%m-%dT%H:%M:%S.%fZ.
For Example: 2019-01-25T15:25:03.881Z

I am converting it to datetime object as follows:

Code:
try:
    datetime_obj = datetime.datetime.strptime(datetime_obj, '%Y-%m-%dT%H:%M:%S.%fZ')
except:
    datetime_obj = datetime.datetime.strptime(datetime_obj, '%Y-%m-%dT%H:%M:%SZ')
datetime_obj = datetime_obj.replace(tzinfo=timezone('UTC'))

But when I try to perform the operation I am greeted with the following error: AttributeError: _nanosecond

Traceback:
File "/env/local/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/batch.py", line 112, in update reference._document_path, field_updates, option File "/env/local/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/_helpers.py", line 822, in pbs_for_update update_pb = extractor.get_update_pb(document_path) File "/env/local/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/_helpers.py", line 459, in get_update_pb name=document_path, fields=encode_dict(self.set_fields) File "/env/local/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/_helpers.py", line 215, in encode_dict return {key: encode_value(value) for key, value in six.iteritems(values_dict)} File "/env/local/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/_helpers.py", line 215, in <dictcomp> return {key: encode_value(value) for key, value in six.iteritems(values_dict)} File "/env/local/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/_helpers.py", line 169, in encode_value return document_pb2.Value(timestamp_value=value.timestamp_pb()) File "/env/local/lib/python3.7/site-packages/google/api_core/datetime_helpers.py", line 278, in timestamp_pb nanos = self._nanosecond or self.microsecond * 1000 AttributeError: _nanosecond

Are datetimes allowed to be set via transactions or I am missing something here?

EDIT:
code snippet:

Code:
@firestore.transactional
def update_datetime_field(transaction, doc_ref, datetime_value):
    try:
        datetime_obj = datetime.datetime.strptime(datetime_value, '%Y-%m-%dT%H:%M:%S.%fZ')
    except:
        datetime_obj = datetime.datetime.strptime(datetime_value, '%Y-%m-%dT%H:%M:%SZ')
    datetime_obj = datetime_obj.replace(tzinfo=timezone('UTC'))
    # Example of datetime_obj -> datetime.datetime(2019, 1, 25, 15, 25, 3, 881000, tzinfo=<UTC>)
    transaction.update(doc_ref, {'datetimeField1': datetime_obj})
    return True

More info:

  1. The above code is triggered when a document is updated at say collection1/document1/collection2/document2
  2. datetime object is python's datetime from the standard libary
  3. I am trying to convert the date to UTC by changing the timezone using pytz

EDIT 2:

A better complete picture:

Code:
from firebase_admin import credentials, firestore

# initialize firebase admin sdk
creds = credentials.ApplicationDefault()
firebase_admin.initialize_app(creds,{'projectId': 'myProjectId'})


@firestore.transactional
def update_datetime_field(transaction, doc_ref, datetime_value):
    try:
        datetime_obj = datetime.datetime.strptime(datetime_value, '%Y-%m-%dT%H:%M:%S.%fZ')
    except:
        datetime_obj = datetime.datetime.strptime(datetime_value, '%Y-%m-%dT%H:%M:%SZ')
    datetime_obj = datetime_obj.replace(tzinfo=timezone('UTC'))
    # Example of datetime_obj -> datetime.datetime(2019, 1, 25, 15, 25, 3, 881000, tzinfo=<UTC>)
    transaction.update(doc_ref, {'datetimeField1': datetime_obj})
    return True

def update_datetime_in_transaction(event, context):
    datetime_value = event['value']['fields']['datetimeField1']['timestampValue']
    # this looks something like 2019-01-25T15:25:03.881Z

    # prepare document reference to document
    doc_ref = prepare_doc_ref(event, context)

    # update_datetime_field
    client = firestore.client()
    transaction = client.transaction()
    update_datetime_field(transaction, doc_ref, datetime_value)

    return True

EDIT 3:

Screenshot of event param: enter image description here

Screenshot of console:
enter image description here
<p>So I am trying to update a datetime field in cloud firestore via cloud function as follows:</p>

<pre><code>transaction.update(doc_ref, {'dateTimeField1': dateTimeValue})
</code></pre>

<p>Google sends datetime objects as string in event param of the cloud function in the format <code>%Y-%m-%dT%H:%M:%SZ</code> or <code>%Y-%m-%dT%H:%M:%S.%fZ</code>.<br>
For Example: <code>2019-01-25T15:25:03.881Z</code></p>

<p>I am converting it to datetime object as follows:</p>

<pre><code>try:
datetime_obj = datetime.datetime.strptime(datetime_obj, '%Y-%m-%dT%H:%M:%S.%fZ')
except:
datetime_obj = datetime.datetime.strptime(datetime_obj, '%Y-%m-%dT%H:%M:%SZ')
datetime_obj = datetime_obj.replace(tzinfo=timezone('UTC'))
</code></pre>

<p>But when I try to perform the operation I am greeted with the following error:
<code>AttributeError: _nanosecond</code></p>

<p>Traceback:<br>
<code>File "/env/local/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/batch.py", line 112, in update
reference._document_path, field_updates, option
File "/env/local/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/_helpers.py", line 822, in pbs_for_update
update_pb = extractor.get_update_pb(document_path)
File "/env/local/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/_helpers.py", line 459, in get_update_pb
name=document_path, fields=encode_dict(self.set_fields)
File "/env/local/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/_helpers.py", line 215, in encode_dict
return {key: encode_value(value) for key, value in six.iteritems(values_dict)}
File "/env/local/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/_helpers.py", line 215, in <dictcomp>
return {key: encode_value(value) for key, value in six.iteritems(values_dict)}
File "/env/local/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/_helpers.py", line 169, in encode_value
return document_pb2.Value(timestamp_value=value.timestamp_pb())
File "/env/local/lib/python3.7/site-packages/google/api_core/datetime_helpers.py", line 278, in timestamp_pb
nanos = self._nanosecond or self.microsecond * 1000
AttributeError: _nanosecond</code></p>

<p>Are datetimes allowed to be set via transactions or I am missing something here?</p>

<p><strong>EDIT:</strong><br>
code snippet:</p>

<pre><code>@firestore.transactional
def update_datetime_field(transaction, doc_ref, datetime_value):
try:
datetime_obj = datetime.datetime.strptime(datetime_value, '%Y-%m-%dT%H:%M:%S.%fZ')
except:
datetime_obj = datetime.datetime.strptime(datetime_value, '%Y-%m-%dT%H:%M:%SZ')
datetime_obj = datetime_obj.replace(tzinfo=timezone('UTC'))
# Example of datetime_obj -> datetime.datetime(2019, 1, 25, 15, 25, 3, 881000, tzinfo=<UTC>)
transaction.update(doc_ref, {'datetimeField1': datetime_obj})
return True
</code></pre>

<p>More info: </p>

<ol>
<li>The above code is triggered when a document is updated at say
<code>collection1/document1/collection2/document2</code></li>
<li>datetime object is python's datetime from the standard libary</li>
<li>I am trying to convert the date to UTC by changing the timezone using pytz</li>
</ol>

<p><strong>EDIT 2:</strong></p>

<p>A better complete picture:</p>

<pre><code>from firebase_admin import credentials, firestore

# initialize firebase admin sdk
creds = credentials.ApplicationDefault()
firebase_admin.initialize_app(creds,{'projectId': 'myProjectId'})


@firestore.transactional
def update_datetime_field(transaction, doc_ref, datetime_value):
try:
datetime_obj = datetime.datetime.strptime(datetime_value, '%Y-%m-%dT%H:%M:%S.%fZ')
except:
datetime_obj = datetime.datetime.strptime(datetime_value, '%Y-%m-%dT%H:%M:%SZ')
datetime_obj = datetime_obj.replace(tzinfo=timezone('UTC'))
# Example of datetime_obj -> datetime.datetime(2019, 1, 25, 15, 25, 3, 881000, tzinfo=<UTC>)
transaction.update(doc_ref, {'datetimeField1': datetime_obj})
return True

def update_datetime_in_transaction(event, context):
datetime_value = event['value']['fields']['datetimeField1']['timestampValue']
# this looks something like 2019-01-25T15:25:03.881Z

# prepare document reference to document
doc_ref = prepare_doc_ref(event, context)

# update_datetime_field
client = firestore.client()
transaction = client.transaction()
update_datetime_field(transaction, doc_ref, datetime_value)

return True
</code></pre>

<p><strong>EDIT 3:</strong></p>

<p>Screenshot of event param:
<a href="https://i.sstatic.net/ynXHE.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ynXHE.png" alt="enter image description here"></a></p>

<p>Screenshot of console:<br>
<a href="https://i.sstatic.net/nHLOw.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/nHLOw.png" alt="enter image description here"></a></p>
 

Latest posts

Online statistics

Members online
1
Guests online
2
Total visitors
3
Top