如何将生成的唯一 ID 从推送功能复制到 child 作为 ID (firebase)

How to duplicate the generated unique ID from push function to child as ID (firebase)

我想知道我如何能够将从 PUSH 生成的唯一 ID 复制到 firebase 数据库到它的 Child 作为 ID object。

见下图示例:

我的第一个方法是生成我自己的唯一 ID,但是,我相信 firebase 的唯一 ID 比创建我自己的更可靠。

this.afd.database.ref().child('Data/'+ MyUniqueID).set(data);

你能建议一下吗?谢谢

请参阅 Update specific fields 网页

  // Get a key for a new Post.
  var newPostKey = firebase.database().ref().child('posts').push().key;

and Getting the unique key generated by push() for nodejs

// Generate a reference to a new location and add some data using push()
var newPostRef = postsRef.push();

// Get the unique key generated by push()
var postId = newPostRef.key;

当您不带任何参数调用 push() 时,它将 return 推送位置的数据库引用。它有一个 key 属性 和在客户端生成的唯一密钥。然后,您可以在随后在该位置写入的对象中使用该键。例如,在普通 javascript(不是 angular)中:

var objectToPush = { data: "foo" };
var ref = parent.push();
objectToPush.id = ref.key;  // add the id to the object to write
ref.set(objectToPush);