显示 Firestore 中其他集合的值时出现问题

问题描述 投票:0回答:1

您好,我正在为我的 IT 学习编写学习项目,但遇到了一些问题。

当我尝试根据公司 uid 和工作场所 uid 显示 Company.Workplace 中的团队时,出现此错误并且不显示任何内容。

enter image description here

这是我在 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)

这是我的 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>

为了更好地识别,我附上了我的 Firestore 底座的图像:

enter image description here

enter image description here

enter image description here

enter image description here

请帮助我,我不知道我应该做什么。

我正在尝试迭代这个集合,但没有成功。

python firebase google-cloud-firestore
1个回答
0
投票

根据官方文档 除 UTF 字符(即 a-z、A-Z、0-9 和下划线 (_))之外的任何内容都需要位于带引号的字段名称中,以反引号字符 (`) 开头和结尾。您在字段名称中使用空格,这些空格需要用反引号转义。

company_id = workplace_doc.get(`'Company ID'`)
workplace_id = workplace_doc.get(`'Workplace ID'`)
© www.soinside.com 2019 - 2024. All rights reserved.