Reputation: 57
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.
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
Upvotes: 1
Views: 420
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