【问题标题】:Can I control the unique key/ID that gets generated when I push an object to Firebase Array?我可以控制将对象推送到 Firebase 阵列时生成的唯一键/ID吗?
【发布时间】:2016-10-02 14:55:11
【问题描述】:

我正在使用 AngularFire $add() 方法,在后台调用 push(),将子对象添加到父对象。

Firebase documentation 表示push() 方法:

使用唯一键生成新的子位置并返回对其的 Firebase 引用。

我想控制为我推送的对象生成的唯一键或 ID。就像我想在随机 ID 前面加上一个常量然后保存它。有可能吗?

【问题讨论】:

  • 目前,据我所知是不可能的
  • 你可以在这里使用 vanilla firebase 还是仅限于使用 AngularFire 的 API?

标签: javascript firebase firebase-realtime-database angularfire


【解决方案1】:

调用push() 是纯客户端操作。所以你可以:

var newRef = ref.push();
var newKey = newRef.key();
var realRef = ref.child("myPrefix_"+newKey);
realRef.set({ title: 'tadaaa', body: 'New child with a push ID and a custom prefix' });

【讨论】:

  • 删除newRef位置的开销,在保存realRef后是多余的。 @Frank 做得很好,谢谢:)
猜你喜欢
  • 2019-02-22
  • 2019-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多