我正在开发一个 Angular 2/Firebase 项目。
我有一个向 firebase 添加新记录的函数:
addItem(newTitle: string, newText: string) {
this.items.push({ title: newTitle, text: newText });
}
这会将类似的内容添加到 firebase 数据库中:
首先它创建一个唯一的 id/key:
-KkztvsKvtqCfoFlpdFc
title: mytitle
text: mytext here
我对其添加的 key 有疑问。它以 - 开头
所以我要么需要一种方法来生成我自己的唯一 id/key,要么让 firebase 不添加 -
我该怎么做?
最佳答案
此处介绍了生成推送 ID 的算法:firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html。代码显示在 this gist .
您可以轻松获取字典 (PUSH_CHARS
) 的 -
并生成没有它们的 key 。
关于Javascript Angular/Firebase 自动键有 "-"删除或添加自己的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44182880/