Home > Software engineering >  I am getting "ValueError: A document must have an even number of path elements"
I am getting "ValueError: A document must have an even number of path elements"

Time:10-25

I am trying to read data from excel and store the text data in firestore. When I try to add it one by one without for loop it is working but if I try to automate the process it is not working.

Working Code:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

cred = credentials.Certificate("certificate.json")
firebase_admin.initialize_app(cred)


db=firestore.client()
name = 'Ashwin'
date = '23/10/2022'
roll = 'sampledata'
cert = 'sampledata'

def store_add(name, date,roll,cert):
    data = {'name':name, 'date':date,'roll':roll}
    db.collection('certs').document(cert).set(data)

store_add(name,date,roll,cert)

This is my code (Not Working):

import pandas as pd
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

cred = credentials.Certificate("certificate.json")
firebase_admin.initialize_app(cred)


db=firestore.client()
def store_add(name, date,roll,cert):
    data = {'name':name, 'date':date,'roll':roll}
    db.collection('certs').document(cert).set(data)

df = pd.read_excel('data.xlsx')
name_list = list(df['name'])
cert = list(df['cert'])
roll = list(df['roll'])
date="24/10/2022"

for i in range(len(name_list)):
    store_add(name_list[i],date,roll[i],cert[i])
    print("Added:",name_list[i])

I am getting the following error:

Traceback (most recent call last):
  File "e:\PSDC\Certificate-Generator\bulk.py", line 23, in <module>
    store_add(name_list[i],date,roll[i],cert[i])
  File "e:\PSDC\Certificate-Generator\bulk.py", line 14, in store_add
    db.collection('certs').document(cert).set(data)
  File "C:\Users\ashwi\AppData\Local\Programs\Python\Python310\lib\site-packages\google\cloud\firestore_v1\base_collection.py", line 130, in document
    return self._client.document(*child_path)
_init__    super(DocumentReference, self).__init__(*path, **kwargs)
  File "C:\Users\ashwi\AppData\Local\Programs\Python\Python310\lib\site-packages\google\cloud\firestore_v1\base_document.py", line 60, in __init__    _helpers.verify_path(path, is_collection=False)
  File "C:\Users\ashwi\AppData\Local\Programs\Python\Python310\lib\site-packages\google\cloud\firestore_v1\_helpers.py", line 150, in verify_path    raise ValueError("A document must have an even number of path elements")
ValueError: A document must have an even number of path elements

Firestore Database Structure (Image)

CodePudding user response:

As mentioned in the documentation, document IDs cannot contain a slash but in the provided screenshot there are 3. The value of cert is PSDC/2022/TCS/01 so the final path becomes certs/PSDC/2022/TCS/01 that has 5 segments i.e. a sub-collection. You can either replace the / with some other character like _:

db.collection('certs').document(cert.replace("/", "_")).set(data)

Alternatively, if the slashes are required, you can store the cert ID in a field in document data and use a random document ID.

  • Related