Firebase: how to generate a unique numeric ID for key?

后端 未结 5 1650
耶瑟儿~
耶瑟儿~ 2020-12-02 13:09

I need numeric IDs for human readability. How do I get it in Firebase?

I want numeric ID for keys, e.g. \"000000001\", \"000000002\",\"00000003\",\"00000004\".

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 13:23

    As the docs say, this can be achieved just by using set instead if push.

    As the docs say, it is not recommended (due to possible overwrite by other user at the "same" time).

    But in some cases it's helpful to have control over the feed's content including keys.

    As an example of webapp in js, 193 being your id generated elsewhere, simply:

     firebase.initializeApp(firebaseConfig);
      var data={
          "name":"Prague"
      };
      firebase.database().ref().child('areas').child("193").set(data);
    

    This will overwrite any area labeled 193 or create one if it's not existing yet.

提交回复
热议问题