user7776690
user7776690

Reputation:

Javascript Angular / Firebase auto keys has "-" remove or add own

I am working on an angular 2 / firebase project.

I have this function which adds a new record to firebase:

addItem(newTitle: string, newText: string) {
    this.items.push({ title: newTitle, text: newText });
  }

This will add something like this to the firebase database:

1st it creates a unique id/key:

-KkztvsKvtqCfoFlpdFc

  title: mytitle

  text: mytext here

I have a problem with the key it add. It starts with a -

So I either need a way to generate my own unique id/key or get firebase to not add the -

How can I do this?

Upvotes: 1

Views: 42

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599011

The algorithm for generating push ids is described here: firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html. The code is shown in this gist.

You can easily take the - our of the dictionary (PUSH_CHARS) and generate keys without them.

Upvotes: 1

Related Questions