【问题标题】:Assigning an automatic ID分配自动 ID
【发布时间】:2018-04-14 11:40:49
【问题描述】:

我正在尝试将数据添加到我的 firebase 数据库。但是如何自动将下一个可用 ID 分配给我要发送的数据?

我通过 Json 导入自动添加了以下数据:

但是,每当我尝试添加数据并将 ID 留空时,我都会得到以下结果:

当我在代码中手动添加 ID 时,它确实有效!但是我怎样才能让它为我自动完成呢?

这是我的消防服务.ts

 create(id: string, description: string, kind: string, name: string, preparation: string, img: string, ingredients: string) {
this.db.object('gerechten/'+id).update({
  id:id,
  description: description,
  kind: kind,
  name: name,
  preparation: preparation,
  img: img,
  ingredients: ingredients
})

}

还有我的 create.ts

  gerecht = {
    description: "",
    kind: "",
    name: "",
    prepartion: "",
    ingredients: "",
    id: "",
    img: ""
  }
---------

  create() {
    this.db.create(this.gerecht.id, this.gerecht.description, this.gerecht.kind, this.gerecht.name, this.gerecht.prepartion, this.gerecht.img, this.gerecht.ingredients) 
    this.navCtrl.push(HomePage)
  }

【问题讨论】:

    标签: firebase ionic-framework firebase-realtime-database ionic3


    【解决方案1】:

    create() 方法中,首先你必须从 gerechten 获取 last node,然后获取对象的 key 并将 key 转换为整数。通过加 1 增加键并将值传递给 this.db.create() 方法。

    Get Last node using limitToLast(1))

    【讨论】:

    • 一切正常,但是当我添加一个项目时。 ID 不刷新。我需要注销并重新登录我的应用程序以使其重置
    • 你说这一切都有效是什么意思?你是用我的方式还是你的方式? @Ramon
    猜你喜欢
    • 2014-04-15
    • 2021-07-19
    • 2021-11-26
    • 1970-01-01
    • 2013-04-07
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    相关资源
    最近更新 更多