Cybs
Cybs

Reputation: 57

Fire store in python filling fields with random data


I have a question about python and firestore, I have conected python to fire store and I can add data however when I try to fill in a field in my database using a Varible all I get in the firebase console is a random string.


My code

import urllib
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
link = "example.com/test1"
f = urllib.urlopen(link)
uw = f.read()
linky = "Example.com/test.txt"
fa = urllib.urlopen(linky)
uname = fa.read()
print(uname)
unname=uname
# Use a service account
uname="hello"
cred = credentials.Certificate('key.json')
firebase_admin.initialize_app(cred)
#uw="hello"
db = firestore.client()
doc_ref = db.collection(u'treasure').document(uw)
doc_ref.set({
    u'found_by': uname
    #u'last': u'Lovelace',
    #u'born': 1815
})
#print(uuname)
print(uname)


Here is my firebase console
Sorry, I don't have the needed reputation to embed an image but here is the link


note
I am trying to load the data to put into the database from a server however I have verified this is not the issue. the first one of these url ib request is getting a name of the document however this works well but the second one is where I get the field data but the problem is not loading it off a server
Thanks!

Upvotes: 1

Views: 420

Answers (1)

snakecharmerb
snakecharmerb

Reputation: 55799

The data is being base64-encoded.

>>> s = 'aGVsbG8='
>>> base64.b64decode(s)
b'hello'

I don't use Firebase/Firestore but if the data is being automatically base64-encoded on write then most likely it will be automatically decoded when read.

If you need to manually decode it, note that base64.b64decode returns bytes, so you need to call .decode() on the bytes to get a str.

This comment on github suggests that adding the u prefix to string literals will make Firestore encode as UTF-8 instead of base64.

So in your example:

uname = u'Hello'

Upvotes: 2

Related Questions