【问题标题】:How to duplicate the generated unique ID from push function to child as ID (firebase)如何将生成的唯一 ID 从推送功能复制到子 ID(firebase)
【发布时间】:2019-02-22 17:04:15
【问题描述】:

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

见下图示例:

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

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

你能建议吗?谢谢

【问题讨论】:

    标签: angular firebase ionic-framework firebase-realtime-database angularfire2


    【解决方案1】:

    请参阅Update specific fields 网页版

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

    Getting the unique key generated by push() 用于 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;
    

    【讨论】:

    • 谢谢@Ron Royston,我能够重新创建离子/角度的答案
    【解决方案2】:

    当您不带参数调用push() 时,它将在推送位置返回一个数据库引用。它有一个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);
    

    【讨论】:

    • 谢谢@Doug Stevenson,我能够重新创建离子/角度的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 2016-07-31
    • 2015-09-22
    • 1970-01-01
    • 2010-11-15
    相关资源
    最近更新 更多