Fahad
Fahad

Reputation: 1

How to remove realtime generate id in firebase react js

enter image description here

const { support } = userData;
const res = fetch("https://j-kik-3f4e1-default-rtdb.firebaseio.com/userDataRecords.json",
  {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      support,
    })
  }

Upvotes: 0

Views: 123

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83163

Generic approach for saving several similar values (i.e. same family of values) under a given node:

Usually, if you want to save several similar values (i.e. same family of values) under a given node without creating sub-nodes with an auto-generated key, I can see the two following options:

Option 1: Use the value as the node keys and assign them a dummy data (e.g. true), as follows:

- parentNode
   - value1: true
   - value2: true
   - value3: true

Option 2: Save the values with sub-node keys that you generate yourself. For example, keys that are numeric and sequential, as follows:

- userdatarecords
   - 1: value1
   - 2: value2
   - 3: value3

Note that by doing this way, you will actually store the data as an Array and this is not recommended in most of the cases, see this official blog article.

Of course you could generate sub-node keys that are not numeric and/or sequential.

Specific case for saving URLs

As Puf mentioned in his comment below, dots are not allowed in RTDB keys. Therefore he advised to "use the URL's hash as the key, and then put the actual URL as its value". You can find a hash function in JavaScript in this SO answer.

therefore Option 1 above would become similar to an option 2 approach, as follows:

- userdatarecords
   - 4340527707825375: https://www.abcd.com
   - 6839177281380098: https://www.efgh.com
   - 228613294339159: https://www.ijkl.com

Upvotes: 1

Related Questions