Skip to content
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

On multiple requests: TypeError: sequence item 1: expected str instance, MetaField found #108

Open
micheledicosmo opened this issue Apr 19, 2021 · 6 comments

Comments

@micheledicosmo
Copy link

The first time I run the following code, it runs ok. The second time I run it without restarting the program, I get this error.

This happens both in debug mode and in production.

class Log(Model):
    id = fields.IDField
    time = fields.DateTime()
    component = fields.TextField()
    message = fields.TextField()
    payload = fields.TextField()
    payload_json = fields.MapField()
    level = fields.TextField()

    class Meta:
        collection_name = "logs"
logger = logging.getLogger(__name__)

def debug(component:str, message:str, payload=None):
    payload_json = None
    if not isinstance(payload, dict):
        try:
            payload_json = json.loads(payload)
        except:
            logger.exception(f'Exception while deserializing a JSON payload: {payload}')
    
    models.Log(
        time=datetime.datetime.now(),
        level='debug',
        component=str(component),
        message=str(message),
        payload=str(payload),
        payload_json=payload_json,
    ).save()
Traceback (most recent call last):
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/workspace/blueprints/....py", line 155, in ...redacted...
    components.dblog.debug(...redacted...)
  File "/workspace/components/dblog.py", line 20, in debug
    models.Log(
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/fireo/models/model.py", line 346, in save
    return self.__class__.collection.create(self, transaction, batch, merge, **self._get_fields())
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/fireo/managers/managers.py", line 230, in create
    return self.queryset.create(mutable_instance, transaction, batch, merge, **field_list)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/fireo/queries/query_set.py", line 53, in create
    return CreateQuery(self.model_cls, mutable_instance, **kwargs).exec(transaction_or_batch, merge)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/fireo/queries/create_query.py", line 151, in exec
    return query_wrapper.ModelWrapper.from_query_result(self.model, self._raw_exec(merge=merge))
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/fireo/queries/create_query.py", line 132, in _raw_exec
    ref = self._doc_ref()
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/fireo/queries/create_query.py", line 73, in _doc_ref
    return self.get_ref().document(self.model._id)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/firestore_v1/collection.py", line 109, in document
    return self._client.document(*child_path)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/firestore_v1/client.py", line 317, in document
    joined_path = _helpers.DOCUMENT_PATH_DELIMITER.join(path)
TypeError: sequence item 1: expected str instance, MetaField found 
@AxeemHaider
Copy link
Contributor

It should have to save. It seems this problem is from Firestore. Try to save it using firestore native lib. And other possible problem is in your data

@IshuSingh1
Copy link

I am also currently stuck due to this error. Were you able to find any fixes OP?

@micheledicosmo
Copy link
Author

micheledicosmo commented Jun 27, 2021 via email

@AxeemHaider
Copy link
Contributor

Please show off your data which you are trying to save.

Below example is working fine for me. Even I try it multiple time.

from fireo import fields
from fireo.models import Model
from datetime import datetime


class Log(Model):
    id = fields.IDField
    time = fields.DateTime()
    component = fields.TextField()
    message = fields.TextField()
    payload = fields.TextField()
    payload_json = fields.MapField()
    level = fields.TextField()

    class Meta:
        collection_name = "logs"

payload_json = {'name': 'json_payload'}

log = Log(
        time=datetime.now(),
        level='debug',
        component=str('component'),
        message=str('message'),
        payload=str('payload'),
        payload_json=payload_json,
    ).save()

print(log.key)

@IshuSingh1
Copy link

IshuSingh1 commented Jun 27, 2021

This is the method I created to add a document

COLLECTION = 'message_ids'
def add_document(obj, username): data = {COLLECTION: get_message_id(obj), 'username': username, 'text': get_message( obj), 'sender_id': get_sender_id(obj), 'recipient_id': get_recipient_id(obj)} db.collection(COLLECTION).document(id).set(data)

FYI I am using native firebase_admin. But the error I am getting is the same error mentioned by OP.
Please have a look and let me know if you see any issues with it.
Thank you

@AxeemHaider
Copy link
Contributor

it seems problem is in data please try to print out data and see if there is any problem or is it support type of Firestore
if you are trying to save it throw native firebase_admin then probably there is problem in data or data type is not support by firestore.

Please try to print data. before saving

print(data)

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

3 participants