Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firestore文档与Python的合并_Python_Firebase_Google Cloud Firestore - Fatal编程技术网

Firestore文档与Python的合并

Firestore文档与Python的合并,python,firebase,google-cloud-firestore,Python,Firebase,Google Cloud Firestore,我需要使用set()和{merge:true}将文档推送到Firebase Firestore数据库。我不能使用update(),因为我不能保证文档预先存在 如果我使用Angular&TypeScript,代码将是: import { AngularFirestore } from '@angular/fire/firestore'; constructor(private firestore: AngularFirestore) { } insertData() { this.fi

我需要使用set()和{merge:true}将文档推送到Firebase Firestore数据库。我不能使用update(),因为我不能保证文档预先存在

如果我使用Angular&TypeScript,代码将是:

import { AngularFirestore } from '@angular/fire/firestore';

constructor(private firestore: AngularFirestore) {
}

insertData() {
    this.firestore.collection('officePlants').doc('docXYZ')
        .set({
            'humidity': this.humiditySensorReading,
            'temperature': this.temperatureSensorReading
        },
            { merge: true });                                                   // provides an update and creates the doc if it doesn't exist
}
我正试图用Python做同样的事情:

from google.cloud import firestore
import firebase_admin                                   # sudo pip install firebase-admin
from firebase_admin import credentials
from firebase_admin import firestore

credentials_path = 'myServiceAccount.privateKey.json'
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credentials_path

cred = credentials.Certificate(credentials_path)
firebase_admin.initialize_app(cred)

db = firestore.Client()
doc_ref = db.collection(u'officePlants').document('docXYZ')
doc_ref.set({
    u'humidity': humidity,
    u'temperature': temperature
}, {'merge': True})
但我得到了一个错误:

ValueError(“无效的合并路径:{}”。格式(合并路径)) ValueError:无效的合并路径:FieldPath('合并')

删除引号时,
{merge:True}
会出现错误

名称错误:未定义名称“合并”


谢谢

Python版本中的语法有点变化:

data = {
    u'humidity': humidity,
    u'temperature': temperature
}

doc_ref = db.collection(u'officePlants').document('docXYZ')
doc_ref.set(data, merge=True)