【问题标题】:How to push multiple new object on firebase (python)如何在firebase(python)上推送多个新对象
【发布时间】:2019-08-19 00:03:01
【问题描述】:

我想在不为每个对象生成新密钥的情况下将数据从 json 推送到 firebase。理想情况下,我想定义密钥的名称。

这是我的代码:

import pyrebase
import json

config = {my firebase config}
firebase = pyrebase.initialize_app(config)
db = firebase.database()

with open('myjson.json') as response:
    data = json.load(response)

db.child("customer").push(data)

我的 json 看起来像这样:

{
  "first_customer": {
    "createdat": "2019-08-09T06:40:10+0000",
    "expires": 1572039000.0,
  },
  "second_customer": {
    "createdat": "2019-08-09T06:33:39+0000",
    "expires": 1570048200.0,
  }
}

但实际上我的代码在 firebase 上给了我这个:

"customer": {
 "-Lm_M6OM7MCV3_PFvcSH(random_id_given_by_firebase)": {
  "first_customer": {
    "createdat": "2019-08-09T06:40:10+0000",
    "expires": 1572039000.0,
  },
  "second_customer": {
    "createdat": "2019-08-09T06:33:39+0000",
    "expires": 1570048200.0,
  }
 }
}

我期望的是:

"customer": {
  "first_customer": {
    "createdat": "2019-08-09T06:40:10+0000",
    "expires": 1572039000.0,
  },
  "second_customer": {
    "createdat": "2019-08-09T06:33:39+0000",
    "expires": 1570048200.0,
  }
}

【问题讨论】:

    标签: python json firebase-realtime-database pyrebase


    【解决方案1】:

    要控制完整路径,请使用set。所以像:

    db.child("customer").child("keyforcustomer").set(data)
    

    【讨论】:

    • 我刚刚做了 db.child("customer").set(data) 并且它有效!感谢您的帮助弗兰克
    猜你喜欢
    • 2019-05-09
    • 1970-01-01
    • 2020-04-17
    • 2017-08-24
    • 2018-10-28
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 2020-11-11
    相关资源
    最近更新 更多