skip to Main Content

24.04 ACTUALIZATION
I managed to fix the problem related to my query: it was enough to change the name of the fields in the document from ‘Company ID’ to ‘CompanyID’ and from ‘Workplace ID’ to ‘WorkplaceID’. But now a new problem has appeared – only one team is displayed despite having 2 documents (uid) in Employee.Workplace any ideas for this situation?
enter image description here

This is my code right now:

endpoint in app.py:

@app.route('/employees_teams', methods=['GET', 'POST'])
@login_required
def employees_teams():
    error_message = None
    team_names = []

    try:
        employee_uid = session.get('user', {}).get('uid')
        employee_workplaces_ref = db.collection('Employee').document(employee_uid).collection('Workplace')
        employee_workplaces = employee_workplaces_ref.stream()

        for workplace_doc in employee_workplaces:
            company_id = workplace_doc.get('CompanyID')
            workplace_id = workplace_doc.get('WorkplaceID')

            # Sprawdź, czy oba pola są niepuste
            if company_id and workplace_id:
                # Pobierz zespół na podstawie Workplace ID w danym Company
                workplace_ref = db.collection('Company').document(company_id).collection('Workplace').document(
                    workplace_id)
                workplace_data = workplace_ref.get().to_dict()

                if workplace_data:
                    team_names.append(workplace_data.get('Team name'))

    except Exception as e:
        error_message = str(e)

    return render_template('employees_teams.html', team_names=team_names, error_message=error_message)

html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Twoje zespoły</title>
    <script src="../static/script/search_script.js" defer></script>
</head>
<body>
<a href="/employee_main_page"><img src="../static/img/logo.PNG" alt="logo" class="company-logo" width="80" height="80"></a>

{#<!-- Wyświetlanie błędu -->#}
{% if error_message %}
    <p style="color: red;">{{ error_message }}</p>
{% endif %}

{% if team_names or team_created %}
    <ul>
        {% for team_name in team_names %}
            <li><a href="{{ url_for('employee_teams_main_page', team_name=team_name) }}">{{ team_name }}</a></li>
        {% endfor %}
    </ul>
{% else %}
    <p>Nie należysz do żadnego zespołu</p>
{% endif %}

</body>
</html>

Hello I’m writing study project for my IT studies and I have a little problem.

When I try to display a team from Company.Workplace based on the Company uid and Workplace uid, I get this error and nothing is displayed.

enter image description here

This is my endpoint in app.py:

@app.route('/employees_teams', methods=['GET', 'POST'])
@login_required
def employees_teams():
    error_message = None
    team_names = []

    try:
        employee_uid = session.get('user', {}).get('uid')
        employee_workplaces_ref = db.collection('Employee').document(employee_uid).collection('Workplace')
        employee_workplaces = employee_workplaces_ref.stream()

        for workplace_doc in employee_workplaces:
            company_id = workplace_doc.get('Company ID')
            workplace_id = workplace_doc.get('Workplace ID')

            # Pobierz zespół na podstawie Workplace ID w danym Company
            workplace_ref = db.collection('Company').document(company_id).collection('Workplace').document(workplace_id)
            workplace_data = workplace_ref.get().to_dict()

            if workplace_data:
                team_names.append(workplace_data.get('Team name'))

    except Exception as e:
        error_message = str(e)

    return render_template('employees_teams.html', team_names=team_names, error_message=error_message)

and this is my html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Twoje zespoły</title>
    <script src="../static/script/search_script.js" defer></script>
</head>
<body>
<a href="/employee_main_page"><img src="../static/img/logo.PNG" alt="logo" class="company-logo" width="80" height="80"></a>

<!-- Wyświetlanie błędu -->
{% if error_message %}
    <p style="color: red;">{{ error_message }}</p>
{% endif %}

{% if team_names %}
    <ul>
        {% for team_name in team_names %}
            <li>{{ team_name }}</li>
        {% endfor %}
    </ul>
{% else %}
    <p>Nie należysz do żadnego zespołu.</p>
{% endif %}

</body>
</html>

For better recognition I’m attaching image of my Firestore base:

enter image description here

enter image description here

enter image description here

enter image description here

Please help me I dont have any idea what I should to do.

I’m trying iterations through this collections and didn’t work.

2

Answers


  1. Chosen as BEST ANSWER

    I solve this problem. What I had to do was tis: if workplace_doc.id == 'defualt': continue and now its displaying all teams from Employee.Workplace. My actual code:

    endpoint:

    @app.route('/employees_teams', methods=['GET', 'POST'])
    @login_required
    def employees_teams():
        error_message = None
        team_names = []
    
        try:
            employee_uid = session.get('user', {}).get('uid')
            employee_workplaces_ref = db.collection('Employee').document(employee_uid).collection('Workplace')
            employee_workplaces = employee_workplaces_ref.stream()
    
            for workplace_doc in employee_workplaces:
                if workplace_doc.id == 'default':
                    continue
                company_id = workplace_doc.get('CompanyID')
                workplace_id = workplace_doc.get('WorkplaceID')
    
                company_workplace_ref = db.collection('Company').document(company_id).collection('Workplace').document(
                    workplace_id)
                company_workplace_data = company_workplace_ref.get().to_dict()
    
                team_name = company_workplace_data.get('Team name')
                if team_name:
                    team_names.append(team_name)
    
        except Exception as e:
            error_message = str(e)
            print(f"Error retrieving data: {e}")
    
        return render_template('employees_teams.html', team_names=team_names, error_message=error_message)
    

    html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Twoje zespoły</title>
        <script src="../static/script/search_script.js" defer></script>
    </head>
    <body>
    <a href="/employee_main_page"><img src="../static/img/logo.PNG" alt="logo" class="company-logo" width="80" height="80"></a>
    
    {#<!-- Wyświetlanie błędu -->#}
    {% if error_message %}
        <p style="color: red;">{{ error_message }}</p>
    {% endif %}
    
    {% if team_names or team_created %}
        <ul>
            {% for team_name in team_names %}
                <li><a href="{{ url_for('employee_teams_main_page', team_name=team_name) }}">{{ team_name }}</a></li>
            {% endfor %}
        </ul>
    {% else %}
        <p>Nie należysz do żadnego zespołu</p>
    {% endif %}
    
    </body>
    </html>
    

    Thats all thank you.


  2. As per official document Anything other than UTF character i.e a-z, A-Z, 0-9, and underscore (_) needs to be in a quoted field name starts and ends with the backtick character (`). You are using spaces in the field name, those Spaces needs to be escaped by backticks.

    I.e

    company_id = workplace_doc.get(`'Company ID'`)
    workplace_id = workplace_doc.get(`'Workplace ID'`)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search